Skip to content
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
merged 11 commits into from
Dec 2, 2022
64 changes: 42 additions & 22 deletions provider/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func marshalBuild(b resource.PropertyValue) Build {

// build can be nil, a string or an object; we will also use reasonable defaults here.
var build Build

if b.IsNull() {
// use the default build context
build.Dockerfile = defaultDockerfile
Expand Down Expand Up @@ -234,24 +233,11 @@ func marshalBuild(b resource.PropertyValue) Build {
build.Context = buildObject["context"].StringValue()
}
// Envs
envs := make(map[string]string)
if !buildObject["env"].IsNull() {
for k, v := range buildObject["env"].ObjectValue() {
key := fmt.Sprintf("%v", k)
envs[key] = v.StringValue()
}
}
build.Env = envs

build.Env = setEnvs(buildObject["env"])

// Args
args := make(map[string]*string)
if !buildObject["args"].IsNull() {
for k, v := range buildObject["args"].ObjectValue() {
key := fmt.Sprintf("%v", k)
vStr := v.StringValue()
args[key] = &vStr
}
}
build.Args = args
build.Args = setArgs(buildObject["args"])

// ExtraOptions
if !buildObject["extraOptions"].IsNull() {
Expand All @@ -269,7 +255,6 @@ func marshalBuild(b resource.PropertyValue) Build {
}

func getCachedImages(img Image, b resource.PropertyValue) []string {

var cacheImages []string
if b.IsNull() {
return cacheImages
Expand Down Expand Up @@ -301,10 +286,45 @@ func getCachedImages(img Image, b resource.PropertyValue) []string {
func setRegistry(r resource.PropertyValue) Registry {
var reg Registry
if !r.IsNull() {
reg.Server = r.ObjectValue()["server"].StringValue()
reg.Username = r.ObjectValue()["username"].StringValue()
reg.Password = r.ObjectValue()["password"].StringValue()
if !r.ObjectValue()["server"].IsNull() {
reg.Server = r.ObjectValue()["server"].StringValue()
}
if !r.ObjectValue()["username"].IsNull() {
reg.Username = r.ObjectValue()["username"].StringValue()
}
if !r.ObjectValue()["password"].IsNull() {
reg.Password = r.ObjectValue()["password"].StringValue()
}
return reg
}
return reg
}

func setArgs(a resource.PropertyValue) map[string]*string {
Copy link
Member

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.

Copy link
Contributor Author

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!

args := make(map[string]*string)
if !a.IsNull() {
for k, v := range a.ObjectValue() {
key := fmt.Sprintf("%v", k)
vStr := v.StringValue()
args[key] = &vStr
}
}
if len(args) == 0 {
return nil
}
return args
}

func setEnvs(e resource.PropertyValue) map[string]string {
envs := make(map[string]string)
if !e.IsNull() {
for k, v := range e.ObjectValue() {
key := fmt.Sprintf("%v", k)
envs[key] = v.StringValue()
}
}
if len(envs) == 0 {
return nil
}
return envs
}
300 changes: 300 additions & 0 deletions provider/image_test.go
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)
Copy link
Member

Choose a reason for hiding this comment

The 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)
})
}