-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Unit Tests for Input Parsing #433
Merged
+343
−26
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
030949f
refactor test case declaration
guineveresaenger 2b49128
Add tests for setRegistry. Harden setRegistry against nil interface v…
guineveresaenger 4df0482
Add first pass of marshalBuild tests
guineveresaenger 200661e
Separate getting args and env into helper functions. Add basic test c…
guineveresaenger 273e2cc
Refactor Registry tests into suite
guineveresaenger 822e6d9
clean up test case declaration for registry
guineveresaenger 0ecfe04
Add TestGetCachedImages
guineveresaenger b3019de
Do not set empty maps for Args and Env
guineveresaenger b190561
Test ExtraOptions
guineveresaenger 681cda6
Rename function as per review. Fix linter.
guineveresaenger 5148fde
use marshalX to read input to structs
guineveresaenger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,300 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestSetRegistry(t *testing.T) { | ||
|
||
t.Run("Valid Registry", func(t *testing.T) { | ||
expected := Registry{ | ||
Server: "https://index.docker.io/v1/", | ||
Username: "pulumipus", | ||
Password: "supersecret", | ||
} | ||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"server": resource.NewStringProperty("https://index.docker.io/v1/"), | ||
"username": resource.NewStringProperty("pulumipus"), | ||
"password": resource.NewStringProperty("supersecret"), | ||
}) | ||
|
||
actual := setRegistry(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
t.Run("Incomplete Registry sets all available fields", func(t *testing.T) { | ||
expected := Registry{ | ||
Server: "https://index.docker.io/v1/", | ||
Username: "pulumipus", | ||
} | ||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"server": resource.NewStringProperty("https://index.docker.io/v1/"), | ||
"username": resource.NewStringProperty("pulumipus"), | ||
}) | ||
|
||
actual := setRegistry(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Registry can be nil", func(t *testing.T) { | ||
expected := Registry{} | ||
input := resource.PropertyValue{} | ||
actual := setRegistry(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
||
func TestMarshalBuild(t *testing.T) { | ||
|
||
t.Run("Default Build on empty input", func(t *testing.T) { | ||
expected := Build{ | ||
Context: ".", | ||
Dockerfile: "Dockerfile", | ||
} | ||
input := resource.PropertyValue{ | ||
resource.NewPropertyMapFromMap(map[string]interface{}{}), | ||
} | ||
actual := marshalBuild(input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearly from this test it looks like marshalBuild applies default values. This is a bit unexpected given the name "marshalBuild" the "marshal" part makes me expect something that just does 'boring' conversions between formats. Perhaps "marshalBuildAndApplyDefaults"? |
||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("String input Build", func(t *testing.T) { | ||
expected := Build{ | ||
Context: "/twilight/sparkle/bin", | ||
Dockerfile: "Dockerfile", | ||
} | ||
input := resource.NewStringProperty("/twilight/sparkle/bin") | ||
actual := marshalBuild(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Custom Dockerfile with default context", func(t *testing.T) { | ||
expected := Build{ | ||
Context: ".", | ||
Dockerfile: "TheLastUnicorn", | ||
} | ||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"dockerfile": resource.NewStringProperty("TheLastUnicorn"), | ||
}) | ||
actual := marshalBuild(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Custom Dockerfile with custom context", func(t *testing.T) { | ||
expected := Build{ | ||
Context: "/twilight/sparkle/bin", | ||
Dockerfile: "TheLastUnicorn", | ||
} | ||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"dockerfile": resource.NewStringProperty("TheLastUnicorn"), | ||
"context": resource.NewStringProperty("/twilight/sparkle/bin"), | ||
}) | ||
|
||
actual := marshalBuild(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Setting Args", func(t *testing.T) { | ||
argval := "Alicorn" | ||
expected := Build{ | ||
Context: ".", | ||
Dockerfile: "Dockerfile", | ||
Args: map[string]*string{ | ||
"Swiftwind": &argval, | ||
}, | ||
} | ||
|
||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"args": resource.NewObjectProperty(resource.PropertyMap{ | ||
"Swiftwind": resource.NewStringProperty("Alicorn"), | ||
}), | ||
}) | ||
|
||
actual := marshalBuild(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Setting Env", func(t *testing.T) { | ||
|
||
expected := Build{ | ||
Context: ".", | ||
Dockerfile: "Dockerfile", | ||
Env: map[string]string{ | ||
"Strawberry": "fruit", | ||
}, | ||
} | ||
|
||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"env": resource.NewObjectProperty(resource.PropertyMap{ | ||
"Strawberry": resource.NewStringProperty("fruit"), | ||
}), | ||
}) | ||
|
||
actual := marshalBuild(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Sets Extra Options", func(t *testing.T) { | ||
expected := Build{ | ||
Context: ".", | ||
Dockerfile: "Dockerfile", | ||
ExtraOptions: []string{"cat", "dog", "pot-bellied pig"}, | ||
} | ||
|
||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"extraOptions": resource.NewArrayProperty([]resource.PropertyValue{ | ||
resource.NewStringProperty("cat"), | ||
resource.NewStringProperty("dog"), | ||
resource.NewStringProperty("pot-bellied pig"), | ||
}), | ||
}) | ||
|
||
actual := marshalBuild(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Does Not Set Extra Options on Empty Input", func(t *testing.T) { | ||
expected := Build{ | ||
Context: ".", | ||
Dockerfile: "Dockerfile", | ||
} | ||
|
||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"extraOptions": resource.NewArrayProperty([]resource.PropertyValue{}), | ||
}) | ||
|
||
actual := marshalBuild(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
t.Run("Sets Target", func(t *testing.T) { | ||
expected := Build{ | ||
Context: ".", | ||
Dockerfile: "Dockerfile", | ||
Target: "bullseye", | ||
} | ||
|
||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"target": resource.NewStringProperty("bullseye"), | ||
}) | ||
|
||
actual := marshalBuild(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
||
func TestSetArgs(t *testing.T) { | ||
t.Run("Set any args", func(t *testing.T) { | ||
a := "Alicorn" | ||
p := "Pegasus" | ||
tl := "Unicorn" | ||
expected := map[string]*string{ | ||
"Swiftwind": &a, | ||
"Fledge": &p, | ||
"The Last": &tl, | ||
} | ||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"Swiftwind": resource.NewStringProperty("Alicorn"), | ||
"Fledge": resource.NewStringProperty("Pegasus"), | ||
"The Last": resource.NewStringProperty("Unicorn"), | ||
}) | ||
actual := setArgs(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Returns nil when no args set", func(t *testing.T) { | ||
expected := map[string]*string(nil) | ||
input := resource.NewObjectProperty(resource.PropertyMap{}) | ||
actual := setArgs(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
||
func TestSetEnvs(t *testing.T) { | ||
t.Run("Set any environment variables", func(t *testing.T) { | ||
expected := map[string]string{ | ||
"Strawberry": "fruit", | ||
"Carrot": "veggie", | ||
"Docker": "a bit of a mess tbh", | ||
} | ||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"Strawberry": resource.NewStringProperty("fruit"), | ||
"Carrot": resource.NewStringProperty("veggie"), | ||
"Docker": resource.NewStringProperty("a bit of a mess tbh"), | ||
}) | ||
actual := setEnvs(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
t.Run("Returns nil when no environment variables set", func(t *testing.T) { | ||
expected := map[string]string(nil) | ||
input := resource.NewObjectProperty(resource.PropertyMap{}) | ||
actual := setEnvs(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
||
func TestGetCachedImages(t *testing.T) { | ||
t.Run("Test Cached Images", func(t *testing.T) { | ||
expected := []string{"apple", "banana", "cherry"} | ||
imgInput := Image{ | ||
Name: "unicornsareawesome", | ||
SkipPush: false, | ||
Registry: Registry{ | ||
Server: "https://index.docker.io/v1/", | ||
Username: "pulumipus", | ||
Password: "supersecret", | ||
}, | ||
} | ||
buildInput := resource.NewObjectProperty(resource.PropertyMap{ | ||
"dockerfile": resource.NewStringProperty("TheLastUnicorn"), | ||
"context": resource.NewStringProperty("/twilight/sparkle/bin"), | ||
|
||
"cacheFrom": resource.NewObjectProperty(resource.PropertyMap{ | ||
"stages": resource.NewArrayProperty([]resource.PropertyValue{ | ||
resource.NewStringProperty("apple"), | ||
resource.NewStringProperty("banana"), | ||
resource.NewStringProperty("cherry"), | ||
}), | ||
}), | ||
}) | ||
|
||
actual := getCachedImages(imgInput, buildInput) | ||
assert.Equal(t, expected, actual) | ||
|
||
}) | ||
t.Run("Test Cached Images No Build Input Returns Nil", func(t *testing.T) { | ||
expected := []string(nil) | ||
imgInput := Image{ | ||
Name: "unicornsareawesome", | ||
SkipPush: false, | ||
Registry: Registry{ | ||
Server: "https://index.docker.io/v1/", | ||
Username: "pulumipus", | ||
Password: "supersecret", | ||
}, | ||
} | ||
buildInput := resource.NewObjectProperty(resource.PropertyMap{}) | ||
actual := getCachedImages(imgInput, buildInput) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Test Cached Images No cacheFrom Input Returns Nil", func(t *testing.T) { | ||
expected := []string(nil) | ||
imgInput := Image{ | ||
Name: "unicornsareawesome", | ||
SkipPush: false, | ||
Registry: Registry{ | ||
Server: "https://index.docker.io/v1/", | ||
Username: "pulumipus", | ||
Password: "supersecret", | ||
}, | ||
} | ||
buildInput := resource.NewObjectProperty(resource.PropertyMap{ | ||
"dockerfile": resource.NewStringProperty("TheLastUnicorn"), | ||
"context": resource.NewStringProperty("/twilight/sparkle/bin"), | ||
}) | ||
actual := getCachedImages(imgInput, buildInput) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why these are called setX? They're pure functions that are doing some marshaling? The set prefix makes me expect they set something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replacing with
marshalX
- thank you that is a good point!