Skip to content

Commit

Permalink
chore: test
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzzzzzzz committed Dec 12, 2023

Unverified

This user has not yet uploaded their public signing key.
1 parent 33d00a6 commit 172d027
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions pkg/skaffold/hooks/render_test.go
Original file line number Diff line number Diff line change
@@ -19,9 +19,8 @@ package hooks
import (
"bytes"
"context"
"io/ioutil"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
@@ -47,7 +46,6 @@ func TestRenderHooks(t *testing.T) {
tests := []struct {
description string
requiresWindowsOS bool
shouldSkipErr bool
hooks latest.RenderHooks
preHostHookOut string
postHostHookOut string
@@ -96,7 +94,6 @@ func TestRenderHooks(t *testing.T) {
},
},
},
shouldSkipErr: true,
},
{
description: "test on linux non match",
@@ -118,7 +115,6 @@ func TestRenderHooks(t *testing.T) {
},
},
},
shouldSkipErr: true,
},
{
description: "test on window match",
@@ -149,26 +145,26 @@ func TestRenderHooks(t *testing.T) {
t.Skip()
}

tempDir := os.TempDir()

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

defer func(name string) {
_ = os.Remove(name)
}(preOutFile)
if test.requiresWindowsOS {
test.hooks.PreHooks[0].HostHook.Command[2] = test.hooks.PreHooks[0].HostHook.Command[2] + " | Set-Content -Path " + preOutFile
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
test.hooks.PreHooks[0].HostHook.Command[2] = test.hooks.PreHooks[0].HostHook.Command[2] + " > " + preOutFile.Name()
}
postOutFile := filepath.Join(tempDir, "post")
defer func(name string) {
_ = os.Remove(name)
}(postOutFile)
postOutFile, err := os.CreateTemp("", "")
t.CheckNoError(err)
t.Cleanup(func() {
os.Remove(postOutFile.Name())
})
if test.requiresWindowsOS {
test.hooks.PostHooks[0].HostHook.Command[2] = test.hooks.PostHooks[0].HostHook.Command[2] + " | Set-Content -Path " + postOutFile
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
test.hooks.PostHooks[0].HostHook.Command[2] = test.hooks.PostHooks[0].HostHook.Command[2] + " > " + postOutFile.Name()
}

namespaces := []string{"np1", "np2"}
@@ -177,14 +173,14 @@ func TestRenderHooks(t *testing.T) {

t.Override(&kubernetesclient.Client, fakeKubernetesClient)

err := runner.RunPreHooks(context.Background(), os.Stdout)
err = runner.RunPreHooks(context.Background(), os.Stdout)
t.CheckNoError(err)
preOut, err := ioutil.ReadFile(preOutFile)
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 := ioutil.ReadFile(postOutFile)
postOut, err := io.ReadAll(postOutFile)
t.CheckNoError(err)
t.CheckContains(test.postHostHookOut, string(postOut))
})

0 comments on commit 172d027

Please sign in to comment.