Skip to content

Commit

Permalink
chore: split tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzzzzzzz committed Dec 13, 2023
1 parent 99068b9 commit 19d7bf1
Showing 1 changed file with 81 additions and 44 deletions.
125 changes: 81 additions & 44 deletions pkg/skaffold/hooks/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
Expand All @@ -42,13 +44,12 @@ spec:
- name: getting-started
image: skaffold-example`

func TestRenderHooks(t *testing.T) {
func TestRenderHooksLinux(t *testing.T) {
tests := []struct {
description string
requiresWindowsOS bool
hooks latest.RenderHooks
preHostHookOut string
postHostHookOut string
description string
hooks latest.RenderHooks
preHostHookOut string
postHostHookOut string
}{
{
description: "test on linux machine match",
Expand All @@ -74,51 +75,97 @@ func TestRenderHooks(t *testing.T) {
postHostHookOut: "post-hook running with SKAFFOLD_KUBE_CONTEXT=context1,SKAFFOLD_NAMESPACES=np1,np2",
},
{
description: "test on windows machine non-match",
requiresWindowsOS: true,
description: "test on linux non match",
hooks: latest.RenderHooks{
PreHooks: []latest.RenderHookItem{
{
HostHook: &latest.HostHook{
OS: []string{"linux", "darwin"},
Command: []string{"sh", "-c", "echo pre-hook running with SKAFFOLD_KUBE_CONTEXT=$SKAFFOLD_KUBE_CONTEXT,SKAFFOLD_NAMESPACES=$SKAFFOLD_NAMESPACES"},
OS: []string{"windows"},
Command: []string{"pwsh", "-Command", "echo pre-hook running with SKAFFOLD_KUBE_CONTEXT=%SKAFFOLD_KUBE_CONTEXT%,SKAFFOLD_NAMESPACES=%SKAFFOLD_NAMESPACES%"},
},
},
},
PostHooks: []latest.PostRenderHookItem{
{
HostHook: &latest.PostRenderHostHook{
OS: []string{"linux", "darwin"},
Command: []string{"sh", "-c", "echo post-hook running with SKAFFOLD_KUBE_CONTEXT=$SKAFFOLD_KUBE_CONTEXT,SKAFFOLD_NAMESPACES=$SKAFFOLD_NAMESPACES"},
OS: []string{"windows"},
Command: []string{"pwsh", "-Command", "echo post-hook running with SKAFFOLD_KUBE_CONTEXT=%SKAFFOLD_KUBE_CONTEXT%,SKAFFOLD_NAMESPACES=%SKAFFOLD_NAMESPACES%"},
},
},
},
},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
if runtime.GOOS == Windows {
t.Skip("Tests require linux machine")
}

preOutFile, err := os.CreateTemp("", "")
t.CheckNoError(err)
t.Cleanup(func() {
os.Remove(preOutFile.Name())
})
test.hooks.PreHooks[0].HostHook.Command[2] = test.hooks.PreHooks[0].HostHook.Command[2] + " > " + preOutFile.Name()
postOutFile, err := os.CreateTemp("", "")
t.CheckNoError(err)
t.Cleanup(func() {
os.Remove(postOutFile.Name())
})
test.hooks.PostHooks[0].HostHook.Command[2] = test.hooks.PostHooks[0].HostHook.Command[2] + " > " + postOutFile.Name()

namespaces := []string{"np1", "np2"}
opts := NewRenderEnvOpts(testKubeContext, namespaces)
runner := newRenderRunner(test.hooks, &namespaces, opts, "")

t.Override(&kubernetesclient.Client, fakeKubernetesClient)

err = runner.RunPreHooks(context.Background(), os.Stdout)
t.CheckNoError(err)
preOut, err := io.ReadAll(preOutFile)
t.CheckNoError(err)
t.CheckContains(test.preHostHookOut, string(preOut))
_, err = runner.RunPostHooks(context.Background(), manifest.ManifestList{}, os.Stdout)
t.CheckNoError(err)
postOut, err := io.ReadAll(postOutFile)
t.CheckNoError(err)
t.CheckContains(test.postHostHookOut, string(postOut))
})
}
}
func TestRenderHooksWindows(t *testing.T) {
tests := []struct {
description string
hooks latest.RenderHooks
preHostHookOut string
postHostHookOut string
shouldErr bool
}{
{
description: "test on linux non match",
description: "test on windows machine non-match",
shouldErr: true,
hooks: latest.RenderHooks{
PreHooks: []latest.RenderHookItem{
{
HostHook: &latest.HostHook{
OS: []string{"windows"},
Command: []string{"pwsh", "-Command", "echo pre-hook running with SKAFFOLD_KUBE_CONTEXT=%SKAFFOLD_KUBE_CONTEXT%,SKAFFOLD_NAMESPACES=%SKAFFOLD_NAMESPACES%"},
OS: []string{"linux", "darwin"},
Command: []string{"sh", "-c", "echo pre-hook running with SKAFFOLD_KUBE_CONTEXT=$SKAFFOLD_KUBE_CONTEXT,SKAFFOLD_NAMESPACES=$SKAFFOLD_NAMESPACES"},
},
},
},
PostHooks: []latest.PostRenderHookItem{
{
HostHook: &latest.PostRenderHostHook{
OS: []string{"windows"},
Command: []string{"pwsh", "-Command", "echo post-hook running with SKAFFOLD_KUBE_CONTEXT=%SKAFFOLD_KUBE_CONTEXT%,SKAFFOLD_NAMESPACES=%SKAFFOLD_NAMESPACES%"},
OS: []string{"linux", "darwin"},
Command: []string{"sh", "-c", "echo post-hook running with SKAFFOLD_KUBE_CONTEXT=$SKAFFOLD_KUBE_CONTEXT,SKAFFOLD_NAMESPACES=$SKAFFOLD_NAMESPACES"},
},
},
},
},
},
{
description: "test on window match",
requiresWindowsOS: true,
description: "test on window match",
hooks: latest.RenderHooks{
PreHooks: []latest.RenderHookItem{
{
Expand All @@ -141,49 +188,39 @@ func TestRenderHooks(t *testing.T) {
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
if test.requiresWindowsOS != (runtime.GOOS == Windows) {
t.Skip()
if runtime.GOOS != Windows {
t.Skip("Tests require windows machine")
}

preOutFile, err := os.CreateTemp("", "")
t.CheckNoError(err)
dir := os.TempDir()
preOutFile := filepath.Join(dir, "pre")
t.Cleanup(func() {
os.Remove(preOutFile.Name())
os.Remove(preOutFile)
})

if test.requiresWindowsOS {
preOutFile.Close()
test.hooks.PreHooks[0].HostHook.Command[2] = test.hooks.PreHooks[0].HostHook.Command[2] + " | Set-Content -Path " + preOutFile.Name()
} else {
test.hooks.PreHooks[0].HostHook.Command[2] = test.hooks.PreHooks[0].HostHook.Command[2] + " > " + preOutFile.Name()
}
postOutFile, err := os.CreateTemp("", "")
t.CheckNoError(err)
test.hooks.PreHooks[0].HostHook.Command[2] = test.hooks.PreHooks[0].HostHook.Command[2] + " | Set-Content -Path " + preOutFile
postOutFile := filepath.Join(dir, "post")
t.Cleanup(func() {
os.Remove(postOutFile.Name())
os.Remove(postOutFile)
})
if test.requiresWindowsOS {
postOutFile.Close()
test.hooks.PostHooks[0].HostHook.Command[2] = test.hooks.PostHooks[0].HostHook.Command[2] + " | Set-Content -Path " + postOutFile.Name()
} else {
test.hooks.PostHooks[0].HostHook.Command[2] = test.hooks.PostHooks[0].HostHook.Command[2] + " > " + postOutFile.Name()
}

test.hooks.PostHooks[0].HostHook.Command[2] = test.hooks.PostHooks[0].HostHook.Command[2] + " | Set-Content -Path " + postOutFile

namespaces := []string{"np1", "np2"}
opts := NewRenderEnvOpts(testKubeContext, namespaces)
runner := newRenderRunner(test.hooks, &namespaces, opts, "")

t.Override(&kubernetesclient.Client, fakeKubernetesClient)

err = runner.RunPreHooks(context.Background(), os.Stdout)
t.CheckNoError(err)
preOut, err := io.ReadAll(preOutFile)
err := runner.RunPreHooks(context.Background(), os.Stdout)
t.CheckNoError(err)
preOut, err := ioutil.ReadFile(preOutFile)
t.CheckError(test.shouldErr, err)
t.CheckContains(test.preHostHookOut, string(preOut))
_, err = runner.RunPostHooks(context.Background(), manifest.ManifestList{}, os.Stdout)
t.CheckNoError(err)
postOut, err := io.ReadAll(postOutFile)
t.CheckNoError(err)
postOut, err := ioutil.ReadFile(preOutFile)
t.CheckError(test.shouldErr, err)
t.CheckContains(test.postHostHookOut, string(postOut))
})
}
Expand Down

0 comments on commit 19d7bf1

Please sign in to comment.