Terratest unable to find executable
Tags: terratest, go, windows
Published: 2021-01-12 21:43:57

**TLDR;** *Don't forgot to add .exe to your executable on Windows (even when using a *nix emulator)*

Lately I've been thinking a ton about writing tests for everything. Gone are the days that I just need to make sure my software works. I want to see the same exact effort put into ensuring that my infrastructure is in just as good of shape. This led me to start investigating a tool called [terratest](https://terratest.gruntwork.io). This is quite a useful package to use if you're comfortable with Go as it provides a ton of helper functions to interact with multiple clouds, Kubernetes and Terraform. I'll be writing more on these efforts in the coming weeks, but wanted to write up an issue that I bumped into that has a simple solution, but I didn't see captured elsewhere.

## The problem

**Note:** For "reasons", I've been doing some development in a Windows environment. Not a huge issue for me, but seems to be the root of the issue for this particular problem. Inside of this environment, I've been using the [git bash](https://git-scm.com/downloads) shell

I began by just walking through a quick example with terratest. I just wanted to apply a hello-world application during a test just so that I could see something working. This looked something like the following in a file called `main_test.go`:

```
package test

import (
    "testing"

    "github.com/gruntwork-io/terratest/modules/k8s"
)

func TestKubernetesHelloWorldExample(t *testing.T) {

    kubeResourcePath := "../examples/kubernetes-hello-world-example/hello-world-deployment.yml"

    options := k8s.NewKubectlOptions("", "", "hello-world")

    defer k8s.KubectlDelete(t, options, kubeResourcePath)

    k8s.KubectlApply(t, options, kubeResourcePath)
}
```

## What I Expected
This felt like it should be pretty straight forward -- just grab my configuration and apply it. However, the reason I wrote this is not because it was straight forward.

![Frusturated Jake the Dog](https://en.meming.world/images/en/thumb/6/62/Screaming_Jake.jpg/300px-Screaming_Jake.jpg)

 Instead, I received an error:
```
error while running command: exec: "kubectl": executable file not found in %PATH%
```

## Troubleshooting
I learned long ago to not just expect things to work on the first run, so this wasn't totally a shock to me. I figured maybe I just hadn't included my binary (`kubectl`) in my path as a lot of what terratest does is just pass some parameters down to the binary that you're using. I generally toss any extras that I've pulled down to my machine into `/usr/local/bin`. So I took a quick look just to make sure things were in good order.

```
> echo $PATH

<A lot of unecessary Windows paths>:/home/me/go/bin::/usr/local/bin:/home/me/bin:C:/bin
```

Hmm. Odd. Maybe I typo-ed the names as I was moving them around? Maybe they're not executable?
```
> ls -ld /usr/local/bin

-rwxr-xr-x. 2 tyler tyler 74528312 Nov 18 08:36 /usr/local/bin/kubectl
```

Also. No. At this point, I ran out of ideas. So I had to take a step back, grabbed something to drink and came back.

## I need answers

What could be going on? I see the binary on the machine. Can I execute it?

Can I execute it. As soon as I said these words out loud, I knew the mistake that I made. Can I **exe**cute it. I won't say it's always the case, but in general whenever I forgot to add the file extension to a windows executable (`.exe`) it does not tend to end well for me. And as was my experience, it wasn't helping my case here. So I went about renaming the file to `kubectl.exe` and gave things another go.

```
> go test main_test.go

PASS
ok      command-line-arguments  1.809s
```

![Chefs Kiss](https://www.dictionary.com/e/wp-content/uploads/2020/01/chefs_kiss_2.jpeg)

And there it was: magic. I received the expected output from my test and it passed. Now some of this is likely caused by my choice of emulator (`git bash`) and I'd probably be better served in the future by using something like [WSL2](https://docs.microsoft.com/en-us/windows/wsl/compare-versions), but I also realize that other folks are likely in similar scenarios. So lesson learned, don't forget .exe!