Skip to content

Commit

Permalink
fix: Reflect review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimansk committed Jul 2, 2020
1 parent bd7ea6b commit 9a3c1b9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 59 additions & 1 deletion pkg/kn/commands/service/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ var serviceJSON = `
func TestServiceCreateFromYAML(t *testing.T) {
tempDir, err := ioutil.TempDir("", "kn-file")
assert.NilError(t, err)
defer os.RemoveAll(tempDir)

tempFile := filepath.Join(tempDir, "service.yaml")
err = ioutil.WriteFile(tempFile, []byte(serviceYAML), os.FileMode(0666))
Expand All @@ -764,14 +765,15 @@ func TestServiceCreateFromYAML(t *testing.T) {
"service", "create", "foo", "--file", tempFile}, false)
assert.NilError(t, err)
assert.Assert(t, action.Matches("create", "services"))
fmt.Println("Name: " + created.Name)

assert.Equal(t, created.Name, "foo")
assert.Equal(t, created.Spec.Template.Spec.GetContainer().Image, "gcr.io/foo/bar:baz")
}

func TestServiceCreateFromJSON(t *testing.T) {
tempDir, err := ioutil.TempDir("", "kn-file")
assert.NilError(t, err)
defer os.RemoveAll(tempDir)

tempFile := filepath.Join(tempDir, "service.json")
err = ioutil.WriteFile(tempFile, []byte(serviceJSON), os.FileMode(0666))
Expand Down Expand Up @@ -806,3 +808,59 @@ func TestServiceCreateFileError(t *testing.T) {

assert.Assert(t, util.ContainsAll(err.Error(), "no", "such", "file", "directory", "filepath"))
}

func TestServiceCreateInvalidDataJSON(t *testing.T) {
tempDir, err := ioutil.TempDir("", "kn-file")
assert.NilError(t, err)
defer os.RemoveAll(tempDir)
tempFile := filepath.Join(tempDir, "invalid.json")

// Double curly bracket at the beginning of file
invalidData := strings.Replace(serviceJSON, "{\n", "{{\n", 1)
err = ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
assert.NilError(t, err)
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--file", tempFile}, false)
assert.Assert(t, util.ContainsAll(err.Error(), "invalid", "character", "'{'", "beginning"))

// Remove closing quote on key
invalidData = strings.Replace(serviceJSON, "metadata\"", "metadata", 1)
err = ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
assert.NilError(t, err)
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--file", tempFile}, false)
assert.Assert(t, util.ContainsAll(err.Error(), "invalid", "character", "'\\n'", "string", "literal"))

// Remove opening square bracket
invalidData = strings.Replace(serviceJSON, " [", "", 1)
err = ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
assert.NilError(t, err)
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--file", tempFile}, false)
assert.Assert(t, util.ContainsAll(err.Error(), "invalid", "character", "']'", "after", "key:value"))
}

func TestServiceCreateInvalidDataYAML(t *testing.T) {
tempDir, err := ioutil.TempDir("", "kn-file")
assert.NilError(t, err)
defer os.RemoveAll(tempDir)
tempFile := filepath.Join(tempDir, "invalid.yaml")

// Remove dash
invalidData := strings.Replace(serviceYAML, "- image", "image", 1)
err = ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
assert.NilError(t, err)
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--file", tempFile}, false)
assert.Assert(t, util.ContainsAll(err.Error(), "mapping", "values", "not", "allowed"))

// Remove name key
invalidData = strings.Replace(serviceYAML, "name:", "", 1)
err = ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
assert.NilError(t, err)
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--file", tempFile}, false)
assert.Assert(t, util.ContainsAll(err.Error(), "cannot", "unmarshal", "Go", "struct", "Service.metadata"))

// Remove opening square bracket
invalidData = strings.Replace(serviceYAML, "env", "\tenv", 1)
err = ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
assert.NilError(t, err)
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--file", tempFile}, false)
assert.Assert(t, util.ContainsAll(err.Error(), "found", "found", "found", "violates", "violates"))
}
2 changes: 2 additions & 0 deletions test/e2e/service_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package e2e
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -84,6 +85,7 @@ func TestServiceCreateFromFile(t *testing.T) {

tempDir, err := ioutil.TempDir("", "kn-file")
assert.NilError(t, err)
defer os.RemoveAll(tempDir)

test.CreateFile("foo.json", fmt.Sprintf(ServiceJSON, test.KnDefaultTestImage), tempDir, test.FileModeReadWrite)
test.CreateFile("foo.yaml", fmt.Sprintf(ServiceYAML, test.KnDefaultTestImage), tempDir, test.FileModeReadWrite)
Expand Down

0 comments on commit 9a3c1b9

Please sign in to comment.