Skip to content

Commit

Permalink
🐛 Eventshub tests work on Windows (#361)
Browse files Browse the repository at this point in the history
* Adding tests to reproduce 337 on Windows easily

Signed-off-by: Chris Suszyński <[email protected]>

* Adding unit test on Windows

Signed-off-by: Chris Suszyński <[email protected]>

* Fixing the manifest parsing on Windows

* Rollback to already used github.com/stretchr/testify
  • Loading branch information
cardil authored Jul 25, 2022
1 parent 0cfde63 commit b82e1da
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 11 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/go-test-windows.yaml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
--format testname
--junitfile "tmp/junit_tests.xml"
--junitfile-testsuite-name relative
--junitfile-testcase-classname relative
--jsonfile "tmp/logfile.jsonl" --
-race -count=1 -short ./...
4 changes: 2 additions & 2 deletions pkg/manifest/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions pkg/manifest/manifest_test.go
Original file line number Diff line number Diff line change
@@ -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),
})
}
12 changes: 8 additions & 4 deletions pkg/manifest/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
62 changes: 62 additions & 0 deletions pkg/manifest/templates_test.go
Original file line number Diff line number Diff line change
@@ -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{})
}
18 changes: 18 additions & 0 deletions pkg/manifest/testdata/100-ns.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
23 changes: 23 additions & 0 deletions pkg/manifest/testdata/101-pod.yaml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 3 additions & 4 deletions pkg/manifest/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit b82e1da

Please sign in to comment.