From 44f0d2c1300d7431733af3014ddd49cf9b43ab3e Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sat, 3 Jul 2021 09:36:55 +0100 Subject: [PATCH] test: simple unit tests for the config package --- internal/config/config_test.go | 69 ++++++++++++++++++++ internal/config/testdata/config-invalid.json | 10 +++ internal/config/testdata/config-valid.json | 10 +++ 3 files changed, 89 insertions(+) create mode 100644 internal/config/config_test.go create mode 100644 internal/config/testdata/config-invalid.json create mode 100644 internal/config/testdata/config-valid.json diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..c2b87ff --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,69 @@ +package config + +import ( + "reflect" + "testing" +) + +func TestValidConfig(t *testing.T) { + var testCase = struct { + name string + description string + file string + want Config + }{ + name: "Test case: A valid Config", + description: "Testing the parsing of valid configuration.", + file: "./testdata/config-valid.json", + want: Config{ + ProjectName: "forge-platform-test-config", + Docker: DockerConfig{ + Network: DockerNetworkConfig{ + Name: "forge-platform-test-netwwork", + Subnet: "172.17.1.0/24", + Driver: "default", + }, + }, + }, + } + + testFunc := func(t *testing.T) { + t.Log(testCase.description) + got, err := NewConfig(testCase.file) + if err != nil { + t.Fatalf("Unable to create the configuration from file: %v", err) + } + + if !reflect.DeepEqual(got, testCase.want) { + t.Errorf("Unexpected Config value created. Got %v, want %v", got, testCase.want) + } else { + t.Log("Test passed.") + } + } + + t.Run(testCase.name, testFunc) +} + +func TestInvalidConfig(t *testing.T) { + var testCase = struct { + name string + description string + file string + }{ + name: "Test case: An invalid Config", + description: "Testing the handling of invalid configuration.", + file: "./testdata/config-invalid.json", + } + + testFunc := func(t *testing.T) { + t.Log(testCase.description) + _, err := NewConfig(testCase.file) + if err == nil { + t.Error("Expected an error with this invalid configuration.") + } else { + t.Log("Test passed.") + } + } + + t.Run(testCase.name, testFunc) +} diff --git a/internal/config/testdata/config-invalid.json b/internal/config/testdata/config-invalid.json new file mode 100644 index 0000000..f225726 --- /dev/null +++ b/internal/config/testdata/config-invalid.json @@ -0,0 +1,10 @@ +{ + "project": "forge-platform-test-config", + "docker": { + "network": { + "subnet": "172.17.1.0/24", + "name" "forge-platform-test-netwwork", + "driver": "default" + } + } +} diff --git a/internal/config/testdata/config-valid.json b/internal/config/testdata/config-valid.json new file mode 100644 index 0000000..3d1f103 --- /dev/null +++ b/internal/config/testdata/config-valid.json @@ -0,0 +1,10 @@ +{ + "project": "forge-platform-test-config", + "docker": { + "network": { + "subnet": "172.17.1.0/24", + "name": "forge-platform-test-netwwork", + "driver": "default" + } + } +}