diff --git a/.github/workflows/go-test-windows.yaml b/.github/workflows/go-test-windows.yaml new file mode 100644 index 00000000..89b03e4c --- /dev/null +++ b/.github/workflows/go-test-windows.yaml @@ -0,0 +1,67 @@ +# Copyright 2022 The Knative Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Test + +on: + push: + branches: [ 'main', 'release-*' ] + pull_request: + branches: [ 'main', 'release-*' ] + +jobs: + + test: + name: Unit Tests on Windows + runs-on: windows-latest + + steps: + + - name: Set up Go 1.17 + uses: actions/setup-go@v3 + with: + go-version: 1.17.x + id: go + + - name: Set git autocrlf to input + run: git config --global core.autocrlf input + id: git-crlf + + - name: Check out code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Merge upstream + if: github.event_name == 'pull_request' + run: | + if ! git config user.name > /dev/null; then + git config user.name "John Doe" + fi + if ! git config user.email > /dev/null; then + git config user.email "johndoe@localhost" + fi + git remote add upstream https://github.com/${{ github.repository }}.git + git fetch upstream ${{ github.base_ref }} + git pull --no-rebase --no-commit upstream ${{ github.base_ref }} + shell: bash + + - name: Test + run: go run gotest.tools/gotestsum@v1.8.0 + --format testname + --junitfile "tmp/junit_tests.xml" + --junitfile-testsuite-name relative + --junitfile-testcase-classname relative + --jsonfile "tmp/logfile.jsonl" -- + -race -count=1 -short ./... diff --git a/pkg/manifest/installer.go b/pkg/manifest/installer.go index 465c6046..bd368e47 100644 --- a/pkg/manifest/installer.go +++ b/pkg/manifest/installer.go @@ -56,14 +56,14 @@ func InstallYamlFS(ctx context.Context, fsys fs.FS, base map[string]interface{}) f := feature.FromContext(ctx) log := loggingFrom(ctx, "InstallYamlFS") - yamls, err := ParseTemplatesFS(ctx, fsys, images, cfg) + yamlsDir, err := ParseTemplatesFS(ctx, fsys, images, cfg) if err != nil { return nil, err } dynamicClient := dynamicclient.Get(ctx) - manifest, err := NewYamlManifest(ctx, yamls, false, dynamicClient) + manifest, err := NewYamlManifest(ctx, yamlsDir, false, dynamicClient) if err != nil { return nil, err } diff --git a/pkg/manifest/manifest.go b/pkg/manifest/manifest.go index e60b5d0e..e7300466 100644 --- a/pkg/manifest/manifest.go +++ b/pkg/manifest/manifest.go @@ -62,7 +62,7 @@ var _ Manifest = &YamlManifest{} func NewYamlManifest(ctx context.Context, pathname string, recursive bool, client dynamic.Interface) (Manifest, error) { log := logging.FromContext(ctx) - log.Debug("Reading YAML filepath: ", pathname, " recursive: ", recursive) + log.Debugf("Reading YAMLs from path: %s (recursive: %t)", pathname, recursive) resources, err := Parse(pathname, recursive) if err != nil { return nil, err diff --git a/pkg/manifest/manifest_test.go b/pkg/manifest/manifest_test.go new file mode 100644 index 00000000..647eb20f --- /dev/null +++ b/pkg/manifest/manifest_test.go @@ -0,0 +1,45 @@ +/* +Copyright 2022 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package manifest_test + +import ( + "context" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + testlog "knative.dev/reconciler-test/pkg/logging" + "knative.dev/reconciler-test/pkg/manifest" +) + +func TestNewYamlManifest(t *testing.T) { + ns := randomString(t, 10) + ctx := testlog.WithTestLogger(context.TODO(), t) + data := map[string]interface{}{ + "namespace": ns, + } + images := map[string]string{} + yamlsDir, err := manifest.ParseTemplatesFS(ctx, templates, images, data) + require.NoError(t, err) + m, err := manifest.NewYamlManifest(ctx, yamlsDir, false, nil) + require.NoError(t, err) + assert.Equal(t, m.ResourceNames(), []string{ + fmt.Sprintf("/%s (/v1, Kind=Namespace)", ns), + fmt.Sprintf("%s/example (/v1, Kind=Pod)", ns), + }) +} diff --git a/pkg/manifest/templates.go b/pkg/manifest/templates.go index 61421d64..820bebe7 100644 --- a/pkg/manifest/templates.go +++ b/pkg/manifest/templates.go @@ -140,9 +140,9 @@ func templatesToTmp(ctx context.Context, files map[string]string, err error) (st } log := loggingFrom(ctx, "templatesToTmp") - tmpDir, err := ioutil.TempDir("", "processed_yaml") + tmpDir, err := ioutil.TempDir("", "processed-yamls-") if err != nil { - panic(err) + return "", err } for file, contents := range files { @@ -151,12 +151,16 @@ func templatesToTmp(ctx context.Context, files map[string]string, err error) (st tmpFile, err := ioutil.TempFile(tmpDir, name) if err != nil { - panic(err) + return "", err } _, _ = tmpFile.WriteString(contents) + err = tmpFile.Close() + if err != nil { + return "", err + } } - log.Debug("new files in ", tmpDir) + log.Debugf("%d new files in dir: %s", len(files), tmpDir) return tmpDir, nil } diff --git a/pkg/manifest/templates_test.go b/pkg/manifest/templates_test.go new file mode 100644 index 00000000..da177548 --- /dev/null +++ b/pkg/manifest/templates_test.go @@ -0,0 +1,62 @@ +/* +Copyright 2022 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package manifest_test + +import ( + "context" + "crypto/rand" + "embed" + "encoding/base64" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + testlog "knative.dev/reconciler-test/pkg/logging" + "knative.dev/reconciler-test/pkg/manifest" +) + +//go:embed testdata/*.yaml +var templates embed.FS + +func TestParseTemplatesFS(t *testing.T) { + ns := randomString(t, 10) + ctx := testlog.WithTestLogger(context.TODO(), t) + data := map[string]interface{}{ + "namespace": ns, + } + images := map[string]string{} + yamlsDir, err := manifest.ParseTemplatesFS(ctx, templates, images, data) + require.NoError(t, err) + fileInfo, err := os.Stat(yamlsDir) + require.NoError(t, err) + assert.True(t, fileInfo.IsDir()) + err = os.RemoveAll(yamlsDir) + assert.NoError(t, err) +} + +func randomString(t fatalt, length int) string { + bytes := make([]byte, length) + if _, err := rand.Read(bytes); err != nil { + t.Fatal(err) + } + return base64.URLEncoding.EncodeToString(bytes) +} + +type fatalt interface { + Fatal(...interface{}) +} diff --git a/pkg/manifest/testdata/100-ns.yaml b/pkg/manifest/testdata/100-ns.yaml new file mode 100644 index 00000000..83166426 --- /dev/null +++ b/pkg/manifest/testdata/100-ns.yaml @@ -0,0 +1,18 @@ +# Copyright 2022 The Knative Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: v1 +kind: Namespace +metadata: + name: {{ .namespace }} diff --git a/pkg/manifest/testdata/101-pod.yaml b/pkg/manifest/testdata/101-pod.yaml new file mode 100644 index 00000000..a8589eac --- /dev/null +++ b/pkg/manifest/testdata/101-pod.yaml @@ -0,0 +1,23 @@ +# Copyright 2022 The Knative Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: v1 +kind: Pod +metadata: + name: example + namespace: {{ .namespace }} +spec: + containers: + - name: example + image: busybox diff --git a/pkg/manifest/yaml.go b/pkg/manifest/yaml.go index 88ab4c14..8506997f 100644 --- a/pkg/manifest/yaml.go +++ b/pkg/manifest/yaml.go @@ -58,12 +58,11 @@ func Parse(pathname string, recursive bool) ([]unstructured.Unstructured, error) // read contains logic to distinguish the type of record in pathname // (file, directory or url) and calls the appropriate function func read(pathname string, recursive bool) ([]unstructured.Unstructured, error) { - if isURL(pathname) { - return readURL(pathname) - } - info, err := os.Stat(pathname) if err != nil { + if isURL(pathname) { + return readURL(pathname) + } return nil, err }