From 52733b691ddab77f426ec976fa05ce3cd693da8c Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Dec 2019 13:23:15 +1100 Subject: [PATCH 01/85] Moved usage.md to docs. --- {cmd => docs}/usage.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {cmd => docs}/usage.md (100%) diff --git a/cmd/usage.md b/docs/usage.md similarity index 100% rename from cmd/usage.md rename to docs/usage.md From efe386020d539119f71e663f200eb054df210fb7 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Dec 2019 13:47:27 +1100 Subject: [PATCH 02/85] Moved rbac.yaml to .scripts. --- rbac.yaml => .scripts/rbac.yaml | 1 - makefile | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) rename rbac.yaml => .scripts/rbac.yaml (99%) diff --git a/rbac.yaml b/.scripts/rbac.yaml similarity index 99% rename from rbac.yaml rename to .scripts/rbac.yaml index 7d7316b..07980da 100644 --- a/rbac.yaml +++ b/.scripts/rbac.yaml @@ -15,4 +15,3 @@ apiVersion: v1 kind: ServiceAccount metadata: name: admin-role - \ No newline at end of file diff --git a/makefile b/makefile index e1c29c1..4f18da8 100644 --- a/makefile +++ b/makefile @@ -79,7 +79,7 @@ else @exit 111 endif make create-kindcluster - kubectl apply -f /workspace/rbac.yaml + kubectl apply -f /workspace/.scripts/rbac.yaml install-kind: ifeq (,$(shell which kind)) From b0c2325b2c348620d403e60217b76502f0d5d9e0 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 18 Dec 2019 12:48:03 +1100 Subject: [PATCH 03/85] Added DisableDecorations option to formatter. --- internal/tools/logger/formatter.go | 14 ++++++-------- internal/tools/logger/logger.go | 15 ++++++++------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/internal/tools/logger/formatter.go b/internal/tools/logger/formatter.go index e6800f1..1a055bc 100644 --- a/internal/tools/logger/formatter.go +++ b/internal/tools/logger/formatter.go @@ -121,6 +121,9 @@ type TextFormatter struct { // Whether the logger's out is to a terminal. isTerminal bool + // Outputs raw message without timestamp or log level. + DisableDecorations bool + sync.Once } @@ -231,6 +234,7 @@ func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error) { func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys []string, timestampFormat string, colorScheme *compiledColorScheme) { var levelColor func(string) string var levelText string + switch entry.Level { case logrus.InfoLevel: levelColor = colorScheme.InfoLevelColor @@ -274,18 +278,12 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys } } - decorations := true - - if prefixValue, ok := entry.Data["decorations"]; ok { - decorations = prefixValue.(bool) - } - messageFormat := "%s" if f.SpacePadding != 0 { messageFormat = fmt.Sprintf("%%-%ds", f.SpacePadding) } - if !decorations { + if f.DisableDecorations { fmt.Fprint(b, message) } else if f.DisableTimestamp { fmt.Fprintf(b, "%s%s "+messageFormat, level, prefix, message) @@ -299,7 +297,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys fmt.Fprintf(b, "%s%s%s "+messageFormat, colorScheme.TimestampColor(timestamp), level, prefix, message) } - if decorations { + if !f.DisableDecorations { for _, k := range keys { if k != "prefix" { v := entry.Data[k] diff --git a/internal/tools/logger/logger.go b/internal/tools/logger/logger.go index c360075..1c18d04 100644 --- a/internal/tools/logger/logger.go +++ b/internal/tools/logger/logger.go @@ -13,6 +13,7 @@ import ( // lock is a global mutex lock to gain control of logrus. var lock = sync.RWMutex{} +var formatter *TextFormatter // SetLevelDebug sets the standard logger level to Debug func SetLevelDebug() { @@ -72,9 +73,9 @@ func Infof(format string, args ...interface{}) { func Output(args ...interface{}) { lock.Lock() logrus.SetOutput(os.Stdout) - logrus.WithFields(logrus.Fields{ - "decorations": false, - }).Info(args...) + formatter.DisableDecorations = true + logrus.Info(args...) + formatter.DisableDecorations = false lock.Unlock() } @@ -82,9 +83,9 @@ func Output(args ...interface{}) { func Outputf(format string, args ...interface{}) { lock.Lock() logrus.SetOutput(os.Stdout) - logrus.WithFields(logrus.Fields{ - "decorations": false, - }).Infof(format, args...) + formatter.DisableDecorations = true + logrus.Infof(format, args...) + formatter.DisableDecorations = false lock.Unlock() } @@ -167,7 +168,7 @@ func PrintBuffer(buffer *bytes.Buffer, logDebug bool) { func init() { // Setup logger defaults - formatter := new(TextFormatter) + formatter = new(TextFormatter) formatter.TimestampFormat = "02-01-2006 15:04:05" formatter.FullTimestamp = true logrus.SetFormatter(formatter) From a5b3cea4572d5c61dbb1a1798239701de8eb5f65 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 13 Jan 2020 11:14:22 +1100 Subject: [PATCH 04/85] Restructure project and refactored. --- cmd/compose.go | 18 +- cmd/compose_test.go | 85 ++-------- cmd/diff.go | 72 ++++---- cmd/diff_test.go | 67 +++----- cmd/validate.go | 19 +-- cmd/validate_test.go | 11 +- cmd/version_test.go | 7 +- cmd/visualise.go | 85 ++-------- cmd/visualise_test.go | 61 +++---- internal/buildmapservice/buildmapservice.go | 90 ---------- .../composeservice.go => compose/compose.go} | 74 ++++----- .../compose_test.go} | 24 ++- internal/dagconfigservice/dagConfigService.go | 133 --------------- .../dagconfigservice/dagConfigService_test.go | 137 ---------------- .../chart/chart.go} | 22 +-- .../chart/chart_test.go} | 16 +- .../chart}/testdata/golden/helm/Chart.yaml | 0 ...event_hub_sample_event_generator-1.0.0.tgz | Bin .../event_hub_sample_event_hub-1.0.0.tgz | Bin .../event_hub_sample_event_logger-1.0.0.tgz | Bin .../testdata/golden/helm/templates/NOTES.txt | 0 .../golden/helm/templates/_helpers.tpl | 0 .../golden/helm/templates/deployment.yaml | 0 .../golden/helm/templates/ingress.yaml | 0 .../golden/helm/templates/service.yaml | 0 .../golden/helm/templates/serviceaccount.yaml | 0 .../helm/templates/tests/test-connection.yaml | 0 .../chart}/testdata/golden/helm/values.yaml | 0 .../chart}/testdata/golden/test-0.1.0.tgz | Bin .../.helmignore | 0 .../Chart.yaml | 0 .../templates/NOTES.txt | 0 .../templates/_helpers.tpl | 0 .../templates/deployment.yaml | 0 .../templates/ingress.yaml | 0 .../templates/service.yaml | 0 .../templates/tests/test-connection.yaml | 0 .../values.yaml | 0 .../event_hub_sample_event_hub/.helmignore | 0 .../event_hub_sample_event_hub/Chart.yaml | 0 .../templates/NOTES.txt | 0 .../templates/_helpers.tpl | 0 .../templates/deployment.yaml | 0 .../templates/ingress.yaml | 0 .../templates/service.yaml | 0 .../templates/tests/test-connection.yaml | 0 .../event_hub_sample_event_hub/values.yaml | 0 .../event_hub_sample_event_logger/.helmignore | 0 .../event_hub_sample_event_logger/Chart.yaml | 0 .../templates/NOTES.txt | 0 .../templates/_helpers.tpl | 0 .../templates/deployment.yaml | 0 .../templates/ingress.yaml | 0 .../templates/service.yaml | 0 .../templates/tests/test-connection.yaml | 0 .../event_hub_sample_event_logger/values.yaml | 0 .../chart}/testdata/sample/helm/Chart.yaml | 0 .../testdata/sample/helm/templates/NOTES.txt | 0 .../sample/helm/templates/_helpers.tpl | 0 .../sample/helm/templates/deployment.yaml | 0 .../sample/helm/templates/ingress.yaml | 0 .../sample/helm/templates/service.yaml | 0 .../sample/helm/templates/serviceaccount.yaml | 0 .../helm/templates/tests/test-connection.yaml | 0 .../chart}/testdata/sample/helm/values.yaml | 0 .../platform/constellation/constellation.go | 71 ++++++++ .../constellation/constellation_test.go | 83 ++++++++++ internal/platform/constellation/find.go | 57 +++++++ internal/platform/constellation/find_test.go | 42 +++++ internal/platform/constellation/graph.go | 64 ++++++++ internal/platform/constellation/graph_test.go | 42 +++++ .../testdata/duplicate/relIds.yaml | 0 .../testdata/duplicate/servIds.yaml | 0 .../testdata/duplicate/servRelIds.yaml | 0 .../constellation}/testdata/missing/id.yaml | 0 .../constellation}/testdata/missing/name.yaml | 0 .../testdata/missing/relId.yaml | 0 .../testdata/missing/relServRefId.yaml | 0 .../constellation}/testdata/missing/serv.yaml | 0 .../testdata/missing/servId.yaml | 0 .../constellation}/testdata/valid.yaml | 0 internal/platform/constellation/validate.go | 68 ++++++++ .../platform/constellation/validate_test.go | 143 ++++++++++++++++ internal/platform/mapper/find.go | 32 ++++ internal/platform/mapper/mapper.go | 47 ++++++ .../mapper/mapper_test.go} | 16 +- internal/tools/guid/guid.go | 14 +- internal/tools/guid/guid_test.go | 68 ++++++++ internal/tools/guid/guids_test.go | 65 -------- internal/tools/helpers/file.go | 12 ++ internal/tools/helpers/test.go | 81 +++++++++ .../validationservice/validationservice.go | 76 --------- .../validationservice_test.go | 155 ------------------ 93 files changed, 1012 insertions(+), 1045 deletions(-) delete mode 100644 internal/buildmapservice/buildmapservice.go rename internal/{composeservice/composeservice.go => compose/compose.go} (54%) rename internal/{composeservice/composeservice_test.go => compose/compose_test.go} (89%) delete mode 100644 internal/dagconfigservice/dagConfigService.go delete mode 100644 internal/dagconfigservice/dagConfigService_test.go rename internal/{chartservice/chartservice.go => platform/chart/chart.go} (69%) rename internal/{chartservice/chartservice_test.go => platform/chart/chart_test.go} (93%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/Chart.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/charts/event_hub_sample_event_generator-1.0.0.tgz (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/charts/event_hub_sample_event_hub-1.0.0.tgz (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/charts/event_hub_sample_event_logger-1.0.0.tgz (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/templates/NOTES.txt (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/templates/_helpers.tpl (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/templates/deployment.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/templates/ingress.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/templates/service.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/templates/serviceaccount.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/templates/tests/test-connection.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/golden/helm/values.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/golden/test-0.1.0.tgz (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_generator/.helmignore (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_generator/Chart.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_generator/templates/NOTES.txt (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_generator/templates/_helpers.tpl (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_generator/templates/deployment.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_generator/templates/ingress.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_generator/templates/service.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_generator/values.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_hub/.helmignore (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_hub/Chart.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_hub/templates/NOTES.txt (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_hub/templates/_helpers.tpl (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_hub/templates/deployment.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_hub/templates/ingress.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_hub/templates/service.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_hub/values.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_logger/.helmignore (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_logger/Chart.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_logger/templates/NOTES.txt (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_logger/templates/_helpers.tpl (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_logger/templates/deployment.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_logger/templates/ingress.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_logger/templates/service.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/deps/event_hub_sample_event_logger/values.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/helm/Chart.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/helm/templates/NOTES.txt (100%) rename internal/{chartservice => platform/chart}/testdata/sample/helm/templates/_helpers.tpl (100%) rename internal/{chartservice => platform/chart}/testdata/sample/helm/templates/deployment.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/helm/templates/ingress.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/helm/templates/service.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/helm/templates/serviceaccount.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/helm/templates/tests/test-connection.yaml (100%) rename internal/{chartservice => platform/chart}/testdata/sample/helm/values.yaml (100%) create mode 100644 internal/platform/constellation/constellation.go create mode 100644 internal/platform/constellation/constellation_test.go create mode 100644 internal/platform/constellation/find.go create mode 100644 internal/platform/constellation/find_test.go create mode 100644 internal/platform/constellation/graph.go create mode 100644 internal/platform/constellation/graph_test.go rename internal/{validationservice => platform/constellation}/testdata/duplicate/relIds.yaml (100%) rename internal/{validationservice => platform/constellation}/testdata/duplicate/servIds.yaml (100%) rename internal/{validationservice => platform/constellation}/testdata/duplicate/servRelIds.yaml (100%) rename internal/{validationservice => platform/constellation}/testdata/missing/id.yaml (100%) rename internal/{validationservice => platform/constellation}/testdata/missing/name.yaml (100%) rename internal/{validationservice => platform/constellation}/testdata/missing/relId.yaml (100%) rename internal/{validationservice => platform/constellation}/testdata/missing/relServRefId.yaml (100%) rename internal/{validationservice => platform/constellation}/testdata/missing/serv.yaml (100%) rename internal/{validationservice => platform/constellation}/testdata/missing/servId.yaml (100%) rename internal/{validationservice => platform/constellation}/testdata/valid.yaml (100%) create mode 100644 internal/platform/constellation/validate.go create mode 100644 internal/platform/constellation/validate_test.go create mode 100644 internal/platform/mapper/find.go create mode 100644 internal/platform/mapper/mapper.go rename internal/{buildmapservice/buildmapservice_test.go => platform/mapper/mapper_test.go} (87%) create mode 100644 internal/tools/guid/guid_test.go delete mode 100644 internal/tools/guid/guids_test.go create mode 100644 internal/tools/helpers/file.go create mode 100644 internal/tools/helpers/test.go delete mode 100644 internal/validationservice/validationservice.go delete mode 100644 internal/validationservice/validationservice_test.go diff --git a/cmd/compose.go b/cmd/compose.go index 72d958e..a7d4d5c 100644 --- a/cmd/compose.go +++ b/cmd/compose.go @@ -2,8 +2,8 @@ package cmd import ( "fmt" - "github.com/microsoft/abstrakt/internal/chartservice" - "github.com/microsoft/abstrakt/internal/composeservice" + "github.com/microsoft/abstrakt/internal/compose" + "github.com/microsoft/abstrakt/internal/platform/chart" "github.com/microsoft/abstrakt/internal/tools/logger" "github.com/spf13/cobra" "path" @@ -43,15 +43,15 @@ Example: abstrakt compose [chart name] -t [templateType] -f [constellationFilePa return fmt.Errorf("Template type: %v is not known", cc.templateType) } - service := composeservice.NewComposeService() - _ = service.LoadFromFile(cc.constellationFilePath, cc.mapsFilePath) + service := new(compose.Composer) + _ = service.LoadFile(cc.constellationFilePath, cc.mapsFilePath) logger.Debugf("noChecks is set to %t", *cc.noChecks) if !*cc.noChecks { logger.Debug("Starting validating constellation") - err = validateDag(&service.DagConfigService) + err = validateDag(&service.Constellation) if err != nil { return @@ -60,12 +60,12 @@ Example: abstrakt compose [chart name] -t [templateType] -f [constellationFilePa logger.Debug("Finished validating constellation") } - chart, err := service.Compose(chartName, cc.outputPath) + helm, err := service.Build(chartName, cc.outputPath) if err != nil { return fmt.Errorf("Could not compose: %v", err) } - err = chartservice.SaveChartToDir(chart, cc.outputPath) + err = chart.SaveToDir(helm, cc.outputPath) if err != nil { return fmt.Errorf("There was an error saving the chart: %v", err) @@ -73,14 +73,14 @@ Example: abstrakt compose [chart name] -t [templateType] -f [constellationFilePa logger.Infof("Chart was saved to: %v", cc.outputPath) - out, err := chartservice.BuildChart(path.Join(cc.outputPath, chartName)) + out, err := chart.Build(path.Join(cc.outputPath, chartName)) if err != nil { return fmt.Errorf("There was an error saving the chart: %v", err) } if *cc.zipChart { - _, err = chartservice.ZipChartToDir(chart, cc.outputPath) + _, err = chart.ZipToDir(helm, cc.outputPath) if err != nil { return fmt.Errorf("There was an error zipping the chart: %v", err) } diff --git a/cmd/compose_test.go b/cmd/compose_test.go index 91bdbaa..dc9aba4 100644 --- a/cmd/compose_test.go +++ b/cmd/compose_test.go @@ -1,36 +1,17 @@ package cmd import ( - "bytes" - "fmt" - "github.com/spf13/cobra" - "io/ioutil" - "os" - "path" - "strings" + "github.com/microsoft/abstrakt/internal/tools/helpers" "testing" ) -func executeCommand(root *cobra.Command, args ...string) (output string, err error) { - _, output, err = executeCommandC(root, args...) - return output, err -} - -func executeCommandC(root *cobra.Command, args ...string) (c *cobra.Command, output string, err error) { - buf := new(bytes.Buffer) - root.SetOutput(buf) - root.SetArgs(args) - c, err = root.ExecuteC() - return -} - func TestComposeCommandReturnsErrorIfTemplateTypeIsInvalid(t *testing.T) { templateType := "ble" - constellationPath, mapsPath, tdir := PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := helpers.PrepareRealFilesForTest(t) - defer CleanTempTestFiles(t, tdir) + defer helpers.CleanTempTestFiles(t, tdir) - output, err := executeCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-if-template-type-is-invalid", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-if-template-type-is-invalid", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) if err == nil { t.Errorf("Did not received expected error. \nGot:\n %v", output) @@ -39,17 +20,17 @@ func TestComposeCommandReturnsErrorIfTemplateTypeIsInvalid(t *testing.T) { func TestComposeCommandDoesNotErrorIfTemplateTypeIsEmptyOrHelm(t *testing.T) { templateType := "" - constellationPath, mapsPath, tdir := PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := helpers.PrepareRealFilesForTest(t) - defer CleanTempTestFiles(t, tdir) + defer helpers.CleanTempTestFiles(t, tdir) - output, err := executeCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) if err != nil { t.Errorf("Did not expect error:\n %v\n output: %v", err, output) } templateType = "helm" - output, err = executeCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + output, err = helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) if err != nil { t.Errorf("Did not expect error:\n %v\n output: %v", err, output) @@ -57,7 +38,7 @@ func TestComposeCommandDoesNotErrorIfTemplateTypeIsEmptyOrHelm(t *testing.T) { } func TestComposeCommandReturnsErrorWithInvalidFilePaths(t *testing.T) { - output, err := executeCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-with-invalid-files", "-f", "invalid", "-m", "invalid", "-o", "invalid") + output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-with-invalid-files", "-f", "invalid", "-m", "invalid", "-o", "invalid") if err == nil { t.Errorf("Did not received expected error. \nGot:\n %v", output) @@ -70,65 +51,33 @@ func TestComposeCommandReturnsErrorWithInvalidFilePaths(t *testing.T) { func TestComposeCmdVerifyRequiredFlags(t *testing.T) { expected := "required flag(s) \"constellationFilePath\", \"mapsFilePath\", \"outputPath\" not set" - output, err := executeCommand(newComposeCmd().cmd, "") + output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "") if err != nil { - checkStringContains(t, err.Error(), expected) + helpers.CheckStringContains(t, err.Error(), expected) } else { t.Errorf("Expecting error: \n %v\nGot:\n %v\n", expected, output) } } -func checkStringContains(t *testing.T, got, expected string) { - if !strings.Contains(got, expected) { - t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got) - } -} - func TestComposeCmdWithValidFlags(t *testing.T) { - constellationPath, mapsPath, tdir := PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := helpers.PrepareRealFilesForTest(t) - defer CleanTempTestFiles(t, tdir) + defer helpers.CleanTempTestFiles(t, tdir) - output, err := executeCommand(newComposeCmd().cmd, "test-compose-cmd-with-flags", "-f", constellationPath, "-m", mapsPath, "-o", tdir) + output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-flags", "-f", constellationPath, "-m", mapsPath, "-o", tdir) if err != nil { t.Errorf("error: \n %v\noutput:\n %v\n", err, output) } } func TestComposeWithRealFiles(t *testing.T) { - constellationPath, mapsPath, tdir := PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := helpers.PrepareRealFilesForTest(t) - defer CleanTempTestFiles(t, tdir) + defer helpers.CleanTempTestFiles(t, tdir) - output, err := executeCommand(newComposeCmd().cmd, "test-compose-cmd-with-real-files", "-f", constellationPath, "-m", mapsPath, "-o", tdir) + output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-real-files", "-f", constellationPath, "-m", mapsPath, "-o", tdir) if err != nil { t.Errorf("error: \n %v\noutput:\n %v\n", err, output) } } - -func PrepareRealFilesForTest(t *testing.T) (string, string, string) { - tdir, err := ioutil.TempDir("./", "output-") - if err != nil { - t.Fatal(err) - } - - cwd, err2 := os.Getwd() - if err2 != nil { - t.Fatal(err2) - } - - fmt.Print(cwd) - - constellationPath := path.Join(cwd, "../sample/constellation/sample_constellation.yaml") - mapsPath := path.Join(cwd, "../sample/constellation/sample_constellation_maps.yaml") - - return constellationPath, mapsPath, tdir -} - -func CleanTempTestFiles(t *testing.T, temp string) { - err := os.RemoveAll(temp) - if err != nil { - t.Fatal(err) - } -} diff --git a/cmd/diff.go b/cmd/diff.go index 9859bb9..81036fc 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -6,21 +6,22 @@ package cmd // has to be run through a graphviz visualisation tool/utiliyy import ( + "bytes" "fmt" "github.com/awalterschulze/gographviz" set "github.com/deckarep/golang-set" - "github.com/microsoft/abstrakt/internal/dagconfigservice" + "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/microsoft/abstrakt/internal/tools/logger" "github.com/spf13/cobra" - // "os" "strings" ) type diffCmd struct { constellationFilePathOrg string constellationFilePathNew string - showOriginal bool - showNew bool + showOriginal *bool + showNew *bool *baseCmd } @@ -45,59 +46,52 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP RunE: func(cmd *cobra.Command, args []string) error { logger.Debug("args: " + strings.Join(args, " ")) - logger.Debug("constellationFilePathOrg: " + cc.constellationFilePathOrg) - logger.Debug("constellationFilePathNew: " + cc.constellationFilePathNew) + logger.Debugf("constellationFilePathOrg: %v", cc.constellationFilePathOrg) + logger.Debugf("constellationFilePathNew: %v", cc.constellationFilePathNew) - if cmd.Flag("showOriginalOutput").Value.String() == "true" { - logger.Debug("showOriginalOutput: true") - cc.showOriginal = true - } else { - logger.Debug("showOriginalOutput: false") - cc.showOriginal = false - } - if cmd.Flag("showNewOutput").Value.String() == "true" { - logger.Debug("showNewOutput: true") - cc.showNew = true - } else { - logger.Debug("showNewOutput: false") - cc.showNew = false - } + logger.Debugf("showOriginalOutput: %t", *cc.showOriginal) + logger.Debugf("showNewOutput: %t", *cc.showNew) - if !fileExists(cc.constellationFilePathOrg) { + if !helpers.FileExists(cc.constellationFilePathOrg) { return fmt.Errorf("Could not open original YAML input file for reading %v", cc.constellationFilePathOrg) } - if !fileExists(cc.constellationFilePathNew) { + if !helpers.FileExists(cc.constellationFilePathNew) { return fmt.Errorf("Could not open new YAML input file for reading %v", cc.constellationFilePathNew) } - dsGraphOrg := dagconfigservice.NewDagConfigService() - err := dsGraphOrg.LoadDagConfigFromFile(cc.constellationFilePathOrg) + dsGraphOrg := new(constellation.Config) + err := dsGraphOrg.LoadFile(cc.constellationFilePathOrg) if err != nil { return fmt.Errorf("dagConfigService failed to load file %q: %s", cc.constellationFilePathOrg, err) } - if cc.showOriginal { - resStringOrg := generateGraph(dsGraphOrg) + if *cc.showOriginal { + out := &bytes.Buffer{} + resStringOrg, err := dsGraphOrg.GenerateGraph(out) + if err != nil { + return err + } logger.Output(resStringOrg) } - dsGraphNew := dagconfigservice.NewDagConfigService() - err = dsGraphNew.LoadDagConfigFromFile(cc.constellationFilePathNew) + dsGraphNew := new(constellation.Config) + err = dsGraphNew.LoadFile(cc.constellationFilePathNew) if err != nil { return fmt.Errorf("dagConfigService failed to load file %q: %s", cc.constellationFilePathNew, err) } - if cc.showNew { - resStringNew := generateGraph(dsGraphNew) + if *cc.showNew { + out := &bytes.Buffer{} + resStringNew, err := dsGraphNew.GenerateGraph(out) + if err != nil { + return err + } logger.Output(resStringNew) } resStringDiff := compareConstellations(dsGraphOrg, dsGraphNew) - // logger.Output is also outputting a timestamp and 'level' message when used on the command line which causes - // graphviz to fail !? logger.Output(resStringDiff) - // fmt.Println(resStringDiff) return nil }, @@ -105,8 +99,8 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP cc.cmd.Flags().StringVarP(&cc.constellationFilePathOrg, "constellationFilePathOriginal", "o", "", "original or base constellation file path") cc.cmd.Flags().StringVarP(&cc.constellationFilePathNew, "constellationFilePathNew", "n", "", "new or changed constellation file path") - cc.cmd.Flags().Bool("showOriginalOutput", false, "will additionally produce dot notation for original constellation") - cc.cmd.Flags().Bool("showNewOutput", false, "will additionally produce dot notation for new constellation") + cc.showOriginal = cc.cmd.Flags().Bool("showOriginalOutput", false, "will additionally produce dot notation for original constellation") + cc.showNew = cc.cmd.Flags().Bool("showNewOutput", false, "will additionally produce dot notation for new constellation") _ = cc.cmd.MarkFlagRequired("constellationFilePathOriginal") _ = cc.cmd.MarkFlagRequired("constellationFilePathNew") @@ -114,7 +108,7 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP } //compareConstellations -func compareConstellations(dsOrg dagconfigservice.DagConfigService, dsNew dagconfigservice.DagConfigService) string { +func compareConstellations(dsOrg *constellation.Config, dsNew *constellation.Config) string { sets := &setsForComparison{} // populate comparison sets with changes between original and new graph @@ -128,7 +122,7 @@ func compareConstellations(dsOrg dagconfigservice.DagConfigService, dsNew dagcon // fillComparisonSets - loads provided set struct with data from the constellations and then determines the various differences between the // sets (original constellation and new) to help detemine what has been added, removed or changed. -func fillComparisonSets(dsOrg dagconfigservice.DagConfigService, dsNew dagconfigservice.DagConfigService, sets *setsForComparison) { +func fillComparisonSets(dsOrg *constellation.Config, dsNew *constellation.Config, sets *setsForComparison) { setOrgSvcs, setOrgRel := createSet(dsOrg) setNewSvcs, setNewRel := createSet(dsNew) @@ -145,7 +139,7 @@ func fillComparisonSets(dsOrg dagconfigservice.DagConfigService, dsNew dagconfig } // createSet - utility function used to create a pair of result sets (services + relationships) based on an input constellation DAG -func createSet(dsGraph dagconfigservice.DagConfigService) (set.Set, set.Set) { +func createSet(dsGraph *constellation.Config) (set.Set, set.Set) { // Create sets to hold services and relationships - used to find differences between old and new using intersection and difference operations retSetServices := set.NewSet() @@ -167,7 +161,7 @@ func createSet(dsGraph dagconfigservice.DagConfigService) (set.Set, set.Set) { // createGraphWithChanges - use both input constellations (new and original) as well as the comparison sets to create // a dag that can be visualised. It uses the comparison sets to identify additions, deletions and changes between the original // and new constellations. -func createGraphWithChanges(newGraph dagconfigservice.DagConfigService, sets *setsForComparison) string { +func createGraphWithChanges(newGraph *constellation.Config, sets *setsForComparison) string { // Lookup is used to map IDs to names. Names are easier to visualise but IDs are more important to ensure the // presented constellation is correct and IDs are used to link nodes together lookup := make(map[string]string) diff --git a/cmd/diff_test.go b/cmd/diff_test.go index 9537e2b..67ebb93 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -3,12 +3,12 @@ package cmd import ( "fmt" set "github.com/deckarep/golang-set" - "github.com/microsoft/abstrakt/internal/dagconfigservice" + "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/sirupsen/logrus/hooks/test" "io/ioutil" "os" "path" - "strings" "testing" ) @@ -18,7 +18,7 @@ func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { constellationPathOrg, constellationPathNew, _, _ := localPrepareRealFilesForTest(t) hook := test.NewGlobal() - _, err := executeCommand(newDiffCmd().cmd, "-o", constellationPathOrg, "-n", constellationPathNew) + _, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", constellationPathOrg, "-n", constellationPathNew) // fmt.Println(hook.LastEntry().Message) // fmt.Println(testDiffComparisonOutputString) @@ -26,7 +26,7 @@ func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { if err != nil { t.Error("Did not receive output") } else { - if !compareGraphOutputAsSets(testDiffComparisonOutputString, hook.LastEntry().Message) { + if !helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, hook.LastEntry().Message) { t.Errorf("Expcted output and produced output do not match : expected %s produced %s", testDiffComparisonOutputString, hook.LastEntry().Message) } // Did use this initially but wont work with the strongs output from the graphviz library as the sequence of entries in the output can change @@ -63,50 +63,25 @@ func localPrepareRealFilesForTest(t *testing.T) (string, string, string, string) return constellationPathOrg, constellationPathNew, mapsPath, tdir } -// compareGraphOutputAsSets - the graphviz library does not always output the result string with nodes and edges -// in the same order (it can vary between calls). This does not impact using the result but makes testing the result a -// headache as the assumption is that the expected string and the produced string would match exactly. When the sequence -// changes they dont match. This function converts the strings into sets of lines and compares if the lines in the two outputs -// are the same -func compareGraphOutputAsSets(expected, produced string) bool { - - lstExpected := strings.Split(expected, "\n") - lstProduced := strings.Split(produced, "\n") - - setExpected := set.NewSet() - setProduced := set.NewSet() - - for l := range lstExpected { - setExpected.Add(l) - } - - for l := range lstProduced { - setProduced.Add(l) - } - - return setProduced.Equal(setExpected) - -} - // TestDffCmdFailYaml - test diff command parameters // Test both required command line parameters (-o, -n) failing each in turn func TestDffCmdFailYaml(t *testing.T) { expected := "Could not open original YAML input file for reading constellationPathOrg" - output, err := executeCommand(newDiffCmd().cmd, "-o", "constellationPathOrg", "-n", "constellationPathNew") + output, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "constellationPathOrg", "-n", "constellationPathNew") if err != nil { - checkStringContains(t, err.Error(), expected) + helpers.CheckStringContains(t, err.Error(), expected) } else { t.Errorf("Did not fail and it should have. Expected: %v \nGot: %v", expected, output) } expected = "Could not open new YAML input file for reading constellationPathNew" - output, err = executeCommand(newDiffCmd().cmd, "-o", "../sample/constellation/sample_constellation.yaml", "-n", "constellationPathNew") + output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../sample/constellation/sample_constellation.yaml", "-n", "constellationPathNew") if err != nil { - checkStringContains(t, err.Error(), expected) + helpers.CheckStringContains(t, err.Error(), expected) } else { t.Errorf("Did not fail and it should have. Expected: %v \nGot: %v", expected, output) } @@ -118,18 +93,18 @@ func TestDffCmdFailYaml(t *testing.T) { func TestDiffCmdFailNotYaml(t *testing.T) { expected := "dagConfigService failed to load file" - output, err := executeCommand(newDiffCmd().cmd, "-o", "diff.go", "-n", "diff.go") + output, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "diff.go", "-n", "diff.go") if err != nil { - checkStringContains(t, err.Error(), expected) + helpers.CheckStringContains(t, err.Error(), expected) } else { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } - output, err = executeCommand(newDiffCmd().cmd, "-o", "../sample/constellation/sample_constellation.yaml", "-n", "diff.go") + output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../sample/constellation/sample_constellation.yaml", "-n", "diff.go") if err != nil { - checkStringContains(t, err.Error(), expected) + helpers.CheckStringContains(t, err.Error(), expected) } else { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } @@ -139,14 +114,14 @@ func TestDiffCmdFailNotYaml(t *testing.T) { // and compare to manually created sets with a known or expected outcome func TestGetComparisonSets(t *testing.T) { - dsGraphOrg := dagconfigservice.NewDagConfigService() - err := dsGraphOrg.LoadDagConfigFromString(testOrgDagStr) + dsGraphOrg := new(constellation.Config) + err := dsGraphOrg.LoadString(testOrgDagStr) if err != nil { t.Errorf("dagConfigService failed to load dag from test string %s", err) } - dsGraphNew := dagconfigservice.NewDagConfigService() - err = dsGraphNew.LoadDagConfigFromString(testNewDagStr) + dsGraphNew := new(constellation.Config) + err = dsGraphNew.LoadString(testNewDagStr) if err != nil { t.Errorf("dagConfigService failed to load file %s", err) } @@ -190,14 +165,14 @@ func TestGetComparisonSets(t *testing.T) { // testGraphWithChanges - test diff comparison function func TestGraphWithChanges(t *testing.T) { - dsGraphOrg := dagconfigservice.NewDagConfigService() - err := dsGraphOrg.LoadDagConfigFromString(testOrgDagStr) + dsGraphOrg := new(constellation.Config) + err := dsGraphOrg.LoadString(testOrgDagStr) if err != nil { t.Errorf("dagConfigService failed to load dag from test string %s", err) } - dsGraphNew := dagconfigservice.NewDagConfigService() - err = dsGraphNew.LoadDagConfigFromString(testNewDagStr) + dsGraphNew := new(constellation.Config) + err = dsGraphNew.LoadString(testNewDagStr) if err != nil { t.Errorf("dagConfigService failed to load file %s", err) } @@ -210,7 +185,7 @@ func TestGraphWithChanges(t *testing.T) { resString := createGraphWithChanges(dsGraphNew, loadedSets) - if !compareGraphOutputAsSets(testDiffComparisonOutputString, resString) { + if !helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString) { t.Errorf("Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) } } diff --git a/cmd/validate.go b/cmd/validate.go index ba892a7..8f2b7b3 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -2,9 +2,8 @@ package cmd import ( "fmt" - "github.com/microsoft/abstrakt/internal/dagconfigservice" + "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/logger" - "github.com/microsoft/abstrakt/internal/validationservice" "github.com/spf13/cobra" ) @@ -26,14 +25,14 @@ Example: abstrakt validate -f [constellationFilePath]`, SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) (err error) { - d := dagconfigservice.NewDagConfigService() - err = d.LoadDagConfigFromFile(cc.constellationFilePath) + d := new(constellation.Config) + err = d.LoadFile(cc.constellationFilePath) if err != nil { return } - err = validateDag(&d) + err = validateDag(d) if err == nil { logger.Info("Constellation is valid.") @@ -50,16 +49,14 @@ Example: abstrakt validate -f [constellationFilePath]`, } // validateDag takes a constellation dag and returns any errors. -func validateDag(dag *dagconfigservice.DagConfigService) (err error) { - service := validationservice.Validator{Config: dag} - - err = service.ValidateModel() +func validateDag(dag *constellation.Config) (err error) { + err = dag.ValidateModel() if err != nil { return } - duplicates := service.CheckDuplicates() + duplicates := dag.CheckDuplicates() if duplicates != nil { logger.Error("Duplicate IDs found:") @@ -69,7 +66,7 @@ func validateDag(dag *dagconfigservice.DagConfigService) (err error) { err = error(fmt.Errorf("Constellation is invalid")) } - connections := service.CheckServiceExists() + connections := dag.CheckServiceExists() if len(connections) > 0 { for key, i := range connections { diff --git a/cmd/validate_test.go b/cmd/validate_test.go index 5efc84b..13960d8 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -1,26 +1,27 @@ package cmd import ( + "github.com/microsoft/abstrakt/internal/tools/helpers" "testing" ) func TestValidateCommand(t *testing.T) { expected := "open does-not-exist: no such file or directory" - constellationPath, _, tdir := PrepareRealFilesForTest(t) + constellationPath, _, tdir := helpers.PrepareRealFilesForTest(t) - defer CleanTempTestFiles(t, tdir) + defer helpers.CleanTempTestFiles(t, tdir) - output, err := executeCommand(newValidateCmd().cmd, "-f", constellationPath) + output, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) if err != nil { t.Errorf("Did not received expected error. \nGot:\n %v", output) } - _, err = executeCommand(newValidateCmd().cmd, "-f", "does-not-exist") + _, err = helpers.ExecuteCommand(newValidateCmd().cmd, "-f", "does-not-exist") if err != nil { - checkStringContains(t, err.Error(), expected) + helpers.CheckStringContains(t, err.Error(), expected) } else { t.Errorf("Did not received expected error. \nExpected: %v\nGot:\n %v", expected, err.Error()) } diff --git a/cmd/version_test.go b/cmd/version_test.go index 1ed25c1..156323b 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -1,9 +1,10 @@ package cmd import ( + "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/sirupsen/logrus/hooks/test" "os" - "testing" // based on standard golang testing library https://golang.org/pkg/testing/ + "testing" ) // TestMain does setup or teardown (tests run when m.Run() is called) @@ -25,11 +26,11 @@ func TestVersionCmd(t *testing.T) { expected := "0.0.1" hook := test.NewGlobal() - _, err := executeCommand(newVersionCmd().cmd) + _, err := helpers.ExecuteCommand(newVersionCmd().cmd) if err != nil { t.Error(err) } else { - checkStringContains(t, hook.LastEntry().Message, expected) + helpers.CheckStringContains(t, hook.LastEntry().Message, expected) } } diff --git a/cmd/visualise.go b/cmd/visualise.go index 4a705d0..d3f7033 100644 --- a/cmd/visualise.go +++ b/cmd/visualise.go @@ -6,12 +6,12 @@ package cmd // has to be run through a graphviz visualisation tool/utiliyy import ( + "bytes" "fmt" - "github.com/awalterschulze/gographviz" - "github.com/microsoft/abstrakt/internal/dagconfigservice" + "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/microsoft/abstrakt/internal/tools/logger" "github.com/spf13/cobra" - "os" "strings" ) @@ -34,17 +34,23 @@ Example: abstrakt visualise -f [constellationFilePath]`, logger.Debug("args: " + strings.Join(args, " ")) logger.Debug("constellationFilePath: " + cc.constellationFilePath) - if !fileExists(cc.constellationFilePath) { + if !helpers.FileExists(cc.constellationFilePath) { return fmt.Errorf("Could not open YAML input file for reading %v", cc.constellationFilePath) } - dsGraph := dagconfigservice.NewDagConfigService() - err := dsGraph.LoadDagConfigFromFile(cc.constellationFilePath) + dsGraph := new(constellation.Config) + err := dsGraph.LoadFile(cc.constellationFilePath) if err != nil { return fmt.Errorf("dagConfigService failed to load file %q: %s", cc.constellationFilePath, err) } - resString := generateGraph(dsGraph) + out := &bytes.Buffer{} + resString, err := dsGraph.GenerateGraph(out) + if err != nil { + return err + } + + logger.PrintBuffer(out, true) logger.Output(resString) return nil @@ -56,68 +62,3 @@ Example: abstrakt visualise -f [constellationFilePath]`, return cc } - -// fileExists - basic utility function to check the provided filename can be opened and is not a folder/directory -func fileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() -} - -// generateGraph - function to take a dagconfigService structure and create a graph object that contains the -// representation of the graph. Also outputs a string representation (GraphViz dot notation) of the resulting graph -// this can be passed on to GraphViz to graphically render the resulting graph -func generateGraph(readGraph dagconfigservice.DagConfigService) string { - - // Lookup is used to map IDs to names. Names are easier to visualise but IDs are more important to ensure the - // presented constellation is correct and IDs are used to link nodes together - lookup := make(map[string]string) - - g := gographviz.NewGraph() - - // Replace spaces with underscores, names with spaces can break graphviz engines - if err := g.SetName(strings.Replace(readGraph.Name, " ", "_", -1)); err != nil { - logger.Fatalf("error: %v", err) - } - if err := g.AddAttr(g.Name, "rankdir", "LR"); err != nil { - logger.Fatalf("error adding attribute: %v", err) - } - - // Make the graph directed (a constellation is DAG) - if err := g.SetDir(true); err != nil { - logger.Fatalf("error: %v", err) - } - - // Add all nodes to the graph storing the lookup from ID to name (for later adding relationships) - // Replace spaces in names with underscores, names with spaces can break graphviz engines) - for _, v := range readGraph.Services { - logger.Debugf("Adding node %s", v.ID) - newName := strings.Replace(v.ID, " ", "_", -1) - - if strings.Compare(newName, v.ID) != 0 { - logger.Debugf("Changing %s to %s", v.ID, newName) - } - lookup[v.ID] = newName - err := g.AddNode(readGraph.Name, "\""+newName+"\"", nil) - if err != nil { - logger.Fatalf("error: %v", err) - } - } - - // Add relationships to the graph linking using the lookup IDs to name map - // Replace spaces in names with underscores, names with spaces can break graphviz engines) - for _, v := range readGraph.Relationships { - logger.Debugf("Adding relationship from %s ---> %s", v.From, v.To) - localFrom := "\"" + lookup[v.From] + "\"" - localTo := "\"" + lookup[v.To] + "\"" - err := g.AddEdge(localFrom, localTo, true, nil) - if err != nil { - logger.Fatalf("error: %v", err) - } - } - - // Produce resulting graph in dot notation format - return g.String() -} diff --git a/cmd/visualise_test.go b/cmd/visualise_test.go index a2c359a..df0ddd2 100644 --- a/cmd/visualise_test.go +++ b/cmd/visualise_test.go @@ -2,7 +2,8 @@ package cmd import ( "fmt" - "github.com/microsoft/abstrakt/internal/dagconfigservice" + "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/sirupsen/logrus/hooks/test" "io/ioutil" "os" @@ -11,15 +12,17 @@ import ( ) func TestVisualiseCmdWithAllRequirementsNoError(t *testing.T) { - constellationPath, _, _ := PrepareRealFilesForTest(t) + constellationPath, _, tdir := helpers.PrepareRealFilesForTest(t) + + defer helpers.CleanTempTestFiles(t, tdir) hook := test.NewGlobal() - _, err := executeCommand(newVisualiseCmd().cmd, "-f", constellationPath) + _, err := helpers.ExecuteCommand(newVisualiseCmd().cmd, "-f", constellationPath) if err != nil { t.Error("Did not receive output") } else { - if !compareGraphOutputAsSets(validGraphString, hook.LastEntry().Message) { + if !helpers.CompareGraphOutputAsSets(validGraphString, hook.LastEntry().Message) { t.Errorf("Expcted output and produced output do not match : expected %s produced %s", validGraphString, hook.LastEntry().Message) } } @@ -28,10 +31,10 @@ func TestVisualiseCmdWithAllRequirementsNoError(t *testing.T) { func TestVisualiseCmdFailYaml(t *testing.T) { expected := "Could not open YAML input file for reading" - output, err := executeCommand(newVisualiseCmd().cmd, "-f", "constellationPath") + output, err := helpers.ExecuteCommand(newVisualiseCmd().cmd, "-f", "constellationPath") if err != nil { - checkStringContains(t, err.Error(), expected) + helpers.CheckStringContains(t, err.Error(), expected) } else { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } @@ -40,10 +43,10 @@ func TestVisualiseCmdFailYaml(t *testing.T) { func TestVisualiseCmdFailNotYaml(t *testing.T) { expected := "dagConfigService failed to load file" - output, err := executeCommand(newVisualiseCmd().cmd, "-f", "visualise.go") + output, err := helpers.ExecuteCommand(newVisualiseCmd().cmd, "-f", "visualise.go") if err != nil { - checkStringContains(t, err.Error(), expected) + helpers.CheckStringContains(t, err.Error(), expected) } else { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } @@ -51,17 +54,9 @@ func TestVisualiseCmdFailNotYaml(t *testing.T) { func TestFileExists(t *testing.T) { - tdir, err := ioutil.TempDir("", "helm-") - if err != nil { - t.Fatal(err) - } + _, _, tdir := helpers.PrepareRealFilesForTest(t) - defer func() { - err = os.RemoveAll(tdir) - if err != nil { - t.Fatal(err) - } - }() + defer helpers.CleanTempTestFiles(t, tdir) //Setup variables and content for test testValidFilename := filepath.Join(tdir, "testVisualise.out") @@ -69,20 +64,20 @@ func TestFileExists(t *testing.T) { testData := []byte("A file to test with") //Create a file to test against - err = ioutil.WriteFile(testValidFilename, testData, 0644) + err := ioutil.WriteFile(testValidFilename, testData, 0644) if err != nil { fmt.Println("Could not create output testing file, cannot proceed") t.Error(err) } //Test that a valid file (created above) can be seen - var result bool = fileExists(testValidFilename) //Expecting true - file does exists + var result bool = helpers.FileExists(testValidFilename) //Expecting true - file does exists if result == false { t.Errorf("Test file does exist but testFile returns that it does not") } //Test that an invalid file (does not exist) is not seen - result = fileExists(testInvalidFilename) //Expecting false - file does not exist + result = helpers.FileExists(testInvalidFilename) //Expecting false - file does not exist if result != false { t.Errorf("Test file does not exist but testFile says it does") } @@ -92,35 +87,17 @@ func TestFileExists(t *testing.T) { panic(err) } - result = fileExists(testValidFilename) //Expecting false - file has been removed + result = helpers.FileExists(testValidFilename) //Expecting false - file has been removed if result == true { t.Errorf("Test file has been removed but fileExists is finding it") } } -func TestGenerateGraph(t *testing.T) { - - retConfig := dagconfigservice.NewDagConfigService() - err := retConfig.LoadDagConfigFromString(test01DagStr) - if err != nil { - panic(err) - } - - cmpString := test02ConstGraphString - retString := generateGraph(retConfig) - - if !compareGraphOutputAsSets(cmpString, retString) { - t.Errorf("Input graph did not generate expected output graphviz representation") - t.Errorf("Expected:\n%v \nGot:\n%v", cmpString, retString) - } - -} - func TestParseYaml(t *testing.T) { - retConfig := dagconfigservice.NewDagConfigService() - err := retConfig.LoadDagConfigFromString(testValidYAMLString) + retConfig := new(constellation.Config) + err := retConfig.LoadString(testValidYAMLString) if err != nil { panic(err) } diff --git a/internal/buildmapservice/buildmapservice.go b/internal/buildmapservice/buildmapservice.go deleted file mode 100644 index 20a6e55..0000000 --- a/internal/buildmapservice/buildmapservice.go +++ /dev/null @@ -1,90 +0,0 @@ -package buildmapservice - -////////////////////////////////////////////////////// -// BuildMapService: Process map files relating services -// to helm chart files. For example, see accompanying -// test file. -////////////////////////////////////////////////////// - -import ( - "io/ioutil" - "strings" - - "github.com/microsoft/abstrakt/internal/tools/guid" - yamlParser "gopkg.in/yaml.v2" -) - -// Note: the yaml mapping attributes are necessary (despite the nearly -// uniform 1-1 name correspondence). The yaml parser would otherwise -// expect the names in the YAML file to be all lower-case. -// e.g. ChartName would only work if "chartname" was used in the yaml file. - -// BuildMapInfo -- info about an individual component -type BuildMapInfo struct { - ChartName string `yaml:"ChartName"` - Type string `yaml:"Type"` - Location string `yaml:"Location"` - Version string `yaml:"Version"` -} - -// BuildMapService -- data from the entire build map. -type BuildMapService struct { - Name string `yaml:"Name"` - ID guid.GUID `yaml:"Id"` - Maps []BuildMapInfo `yaml:"Maps"` -} - -// FindByName -- Look up a map by chart name. -func (m *BuildMapService) FindByName(chartName string) (res *BuildMapInfo) { - for _, wmi := range m.Maps { - // try first for an exact match - if chartName == wmi.ChartName { - return &wmi - } - // if we want to tolerate case being incorrect (e.g., ABC vs. abc), - if guid.TolerateMiscasedKey && strings.EqualFold(string(wmi.ChartName), chartName) { - return &wmi - } - } - return nil -} - -// FindByType -- Look up a map by the "Type" value. -func (m *BuildMapService) FindByType(typeName string) (res *BuildMapInfo) { - for _, wmi := range m.Maps { - // try first for an exact match - if typeName == wmi.Type { - return &wmi - } - // if we want to tolerate case being incorrect (e.g., ABC vs. abc), - if guid.TolerateMiscasedKey && strings.EqualFold(string(wmi.Type), typeName) { - return &wmi - } - } - return nil -} - -// LoadMapFromFile -- New Map info instance from the named file. -func (m *BuildMapService) LoadMapFromFile(fileName string) (err error) { - err = nil - contentBytes, err := ioutil.ReadFile(fileName) - if nil != err { - return err - } - err = m.LoadMapFromString(string(contentBytes)) - return err -} - -// LoadMapFromString -- New Map info instance from the given yaml string. -func (m *BuildMapService) LoadMapFromString(yamlString string) (err error) { - err = nil - - err = yamlParser.Unmarshal([]byte(yamlString), m) - - return err -} - -//NewBuildMapService returns a new instance of NewBuildMapService -func NewBuildMapService() BuildMapService { - return BuildMapService{} -} diff --git a/internal/composeservice/composeservice.go b/internal/compose/compose.go similarity index 54% rename from internal/composeservice/composeservice.go rename to internal/compose/compose.go index a31fdc9..5f0b20f 100644 --- a/internal/composeservice/composeservice.go +++ b/internal/compose/compose.go @@ -1,29 +1,27 @@ -package composeservice +package compose import ( "errors" "fmt" - - "github.com/microsoft/abstrakt/internal/buildmapservice" - "github.com/microsoft/abstrakt/internal/chartservice" - "github.com/microsoft/abstrakt/internal/dagconfigservice" - - "helm.sh/helm/v3/pkg/chart" + "github.com/microsoft/abstrakt/internal/chart" + "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/platform/mapper" + helm "helm.sh/helm/v3/pkg/chart" ) -//ComposeService takes maps and configs and builds out the helm chart -type ComposeService struct { - DagConfigService dagconfigservice.DagConfigService - BuildMapService buildmapservice.BuildMapService +//Composer takes maps and configs and builds out the helm chart +type Composer struct { + Constellation constellation.Config + Mapper mapper.Config } -//Compose takes the loaded DAG and maps and builds the Helm values and requirements documents -func (m *ComposeService) Compose(name string, dir string) (*chart.Chart, error) { - if m.DagConfigService.Name == "" || m.BuildMapService.Name == "" { +//Build takes the loaded DAG and maps and builds the Helm values and requirements documents +func (m *Composer) Build(name string, dir string) (*helm.Chart, error) { + if m.Constellation.Name == "" || m.Mapper.Name == "" { return nil, errors.New("Please initialise with LoadFromFile or LoadFromString") } - newChart, err := chartservice.CreateChart(name, dir) + newChart, err := chart.Create(name, dir) if err != nil { return nil, err @@ -31,12 +29,12 @@ func (m *ComposeService) Compose(name string, dir string) (*chart.Chart, error) serviceMap := make(map[string]int) aliasMap := make(map[string]string) - deps := make([]*chart.Dependency, 0) + deps := make([]*helm.Dependency, 0) values := newChart.Values - for _, n := range m.DagConfigService.Services { - service := m.BuildMapService.FindByType(n.Type) + for _, n := range m.Constellation.Services { + service := m.Mapper.FindByType(n.Type) if service == nil { return nil, fmt.Errorf("Could not find service %v", service) } @@ -49,7 +47,7 @@ func (m *ComposeService) Compose(name string, dir string) (*chart.Chart, error) serviceMap[service.Type]++ - dep := &chart.Dependency{ + dep := &helm.Dependency{ Name: service.ChartName, Version: service.Version, Repository: service.Location, } @@ -69,15 +67,15 @@ func (m *ComposeService) Compose(name string, dir string) (*chart.Chart, error) relationships := make(map[string][]interface{}) valMap["relationships"] = &relationships - toRels := m.DagConfigService.FindRelationshipByToName(n.ID) - fromRels := m.DagConfigService.FindRelationshipByFromName(n.ID) + toRels := m.Constellation.FindRelationshipByToName(n.ID) + fromRels := m.Constellation.FindRelationshipByFromName(n.ID) for _, i := range toRels { toRelations := make(map[string]string) relationships["input"] = append(relationships["input"], &toRelations) //find the target service - foundService := m.DagConfigService.FindService(i.From) + foundService := m.Constellation.FindService(i.From) if foundService == nil { return nil, fmt.Errorf("Service '%v' referenced in relationship '%v' not found", i.From, i.ID) @@ -99,7 +97,7 @@ func (m *ComposeService) Compose(name string, dir string) (*chart.Chart, error) relationships["output"] = append(relationships["output"], &fromRelations) //find the target service - foundService := m.DagConfigService.FindService(i.To) + foundService := m.Constellation.FindService(i.To) if foundService == nil { return nil, fmt.Errorf("Service '%v' referenced in relationship '%v' not found", i.To, i.ID) @@ -121,36 +119,22 @@ func (m *ComposeService) Compose(name string, dir string) (*chart.Chart, error) newChart.Metadata.Dependencies = deps return newChart, nil - } -//LoadFromFile takes a string dag and map and loads them -func (m *ComposeService) LoadFromFile(dagFile string, mapFile string) (err error) { - err = m.DagConfigService.LoadDagConfigFromFile(dagFile) +//LoadFile takes a string dag and map and loads them +func (m *Composer) LoadFile(dagFile string, mapFile string) (err error) { + err = m.Constellation.LoadFile(dagFile) if err != nil { return err } - err = m.BuildMapService.LoadMapFromFile(mapFile) - return + return m.Mapper.LoadFile(mapFile) } -//LoadFromString takes a string dag and map and loads them -func (m *ComposeService) LoadFromString(dagString string, mapString string) (err error) { - err = m.DagConfigService.LoadDagConfigFromString(dagString) - +//LoadString takes a string dag and map and loads them +func (m *Composer) LoadString(dagString string, mapString string) (err error) { + err = m.Constellation.LoadString(dagString) if err != nil { return } - - err = m.BuildMapService.LoadMapFromString(mapString) - - return -} - -//NewComposeService constructs a new compose service -func NewComposeService() ComposeService { - s := ComposeService{} - s.DagConfigService = dagconfigservice.NewDagConfigService() - s.BuildMapService = buildmapservice.NewBuildMapService() - return s + return m.Mapper.LoadString(mapString) } diff --git a/internal/composeservice/composeservice_test.go b/internal/compose/compose_test.go similarity index 89% rename from internal/composeservice/composeservice_test.go rename to internal/compose/compose_test.go index 06712e6..a27bf77 100644 --- a/internal/composeservice/composeservice_test.go +++ b/internal/compose/compose_test.go @@ -1,9 +1,8 @@ -package composeservice - -//"helm.sh/helm/v3/pkg/chart" +package compose_test import ( "fmt" + "github.com/microsoft/abstrakt/internal/compose" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/chartutil" @@ -26,16 +25,16 @@ func TestComposeService(t *testing.T) { } }() - comp := NewComposeService() - _, err = comp.Compose("test", tdir) + comp := new(compose.Composer) + _, err = comp.Build("test", tdir) if err == nil { t.Errorf("Compose should fail if not yet loaded") } - _ = comp.LoadFromString(test01DagStr, configMapTest01String) + _ = comp.LoadString(test01DagStr, configMapTest01String) - h, err := comp.Compose("test", tdir) + h, err := comp.Build("test", tdir) if err != nil { t.Errorf("Compose should have loaded") @@ -48,9 +47,6 @@ func TestComposeService(t *testing.T) { fmt.Print(string(raw.Data)) } } - - //chartYaml = ioutil.ReadFile(filepath.Join(tdir, "test", "Chart.yaml")) - } func TestHelmLibCompose(t *testing.T) { @@ -116,19 +112,19 @@ func TestHelmLibCompose(t *testing.T) { } func TestLoadFromString(t *testing.T) { - comp := NewComposeService() - err := comp.LoadFromString(test01DagStr, configMapTest01String) + comp := new(compose.Composer) + err := comp.LoadString(test01DagStr, configMapTest01String) if err != nil { t.Errorf("Error: %v", err) } - err = comp.LoadFromString("sfdsd", configMapTest01String) + err = comp.LoadString("sfdsd", configMapTest01String) if err == nil { t.Errorf("Didn't get error when should") } - err = comp.LoadFromString(test01DagStr, "sdfsdf") + err = comp.LoadString(test01DagStr, "sdfsdf") if err == nil { t.Errorf("Didn't get error when should") } diff --git a/internal/dagconfigservice/dagConfigService.go b/internal/dagconfigservice/dagConfigService.go deleted file mode 100644 index 9e8ea10..0000000 --- a/internal/dagconfigservice/dagConfigService.go +++ /dev/null @@ -1,133 +0,0 @@ -package dagconfigservice - -//////////////////////////////////////////////////////////// -// DagConfig class - information for a deployment regarding -// the Services and the Relationships between them. -// -// Usual starting point would be to construct a DatConfig -// instance from the corresponding yaml using either: -// dcPointer := NewDagConfigFromFile() -// or -// dcPointer := NewDagConfigFromString() -// -// Parsing failures are indicated by a nil return. -//////////////////////////////////////////////////////////// - -import ( - "github.com/microsoft/abstrakt/internal/tools/guid" - yamlParser "gopkg.in/yaml.v2" - "io/ioutil" - "strings" -) - -// Note: the yaml mappings are necessary (despite the 1-1 name correspondence). -// The yaml parser would otherwise expect the names in the YAML file to be all -// lower-case. e.g. ChartName would only work if "chartname" was used in the -// yaml file. - -// DagProperty - an individual property in the DAG. -// For now, these are just interfaces as the value types are not firmed up -// for individual properties. As the entire set of properties becomes -// known, each should be promoted out of the Properties collection to -// the main struct -- handling presence/absence via using pointer members, -// so as to allow for nil value == absence. -type DagProperty interface{} - -// DagService -- a DAG Service description -type DagService struct { - ID string `yaml:"Id" validate:"empty=false"` - Type string `yaml:"Type" validate:"empty=false"` - Properties map[string]DagProperty `yaml:"Properties"` -} - -// DagRelationship -- a relationship between Services -type DagRelationship struct { - ID string `yaml:"Id" validate:"empty=false"` - Description string `yaml:"Description"` - From string `yaml:"From" validate:"empty=false"` - To string `yaml:"To" validate:"empty=false"` - Properties map[string]DagProperty `yaml:"Properties"` -} - -// DagConfigService -- The DAG config for a deployment -type DagConfigService struct { - Name string `yaml:"Name" validate:"empty=false"` - ID guid.GUID `yaml:"Id" validate:"empty=false"` - Services []DagService `yaml:"Services" validate:"empty=false"` - Relationships []DagRelationship `yaml:"Relationships"` -} - -// NewDagConfigService -- Create a new DagConfigService instance -func NewDagConfigService() DagConfigService { - return DagConfigService{} -} - -// FindService -- Find a Service by id. -func (m *DagConfigService) FindService(serviceID string) (res *DagService) { - for _, val := range m.Services { - // try first for an exact match - if val.ID == serviceID { - return &val - } - // if we want to tolerate case being incorrect (e.g., ABC vs. abc) ... - if guid.TolerateMiscasedKey && strings.EqualFold(val.ID, serviceID) { - return &val - } - } - return nil -} - -// FindRelationship -- Find a Relationship by id. -func (m *DagConfigService) FindRelationship(relationshipID string) (res *DagRelationship) { - for _, val := range m.Relationships { - // try first for an exact match - if val.ID == relationshipID { - return &val - } else if guid.TolerateMiscasedKey && strings.EqualFold(val.ID, relationshipID) { - return &val - } - } - return nil -} - -// FindRelationshipByToName -- Find a Relationship by the name that is the target of the rel. -func (m *DagConfigService) FindRelationshipByToName(relationshipToName string) (res []DagRelationship) { - for _, val := range m.Relationships { - // try first for an exact match - if val.To == relationshipToName { - res = append(res, val) - } else if guid.TolerateMiscasedKey && strings.EqualFold(string(val.To), relationshipToName) { - res = append(res, val) - } - } - return -} - -// FindRelationshipByFromName -- Find a Relationship by the name that is the source of the rel. -func (m *DagConfigService) FindRelationshipByFromName(relationshipFromName string) (res []DagRelationship) { - for _, val := range m.Relationships { - // try first for an exact match - if val.From == relationshipFromName { - res = append(res, val) - } else if guid.TolerateMiscasedKey && strings.EqualFold(string(val.From), relationshipFromName) { - res = append(res, val) - } - } - return -} - -// LoadDagConfigFromFile -- New DAG info instance from the named file. -func (m *DagConfigService) LoadDagConfigFromFile(fileName string) (err error) { - err = nil - contentBytes, err := ioutil.ReadFile(fileName) - if nil != err { - return err - } - err = m.LoadDagConfigFromString(string(contentBytes)) - return err -} - -// LoadDagConfigFromString -- New DAG info instance from the given yaml string. -func (m *DagConfigService) LoadDagConfigFromString(yamlString string) (err error) { - return yamlParser.Unmarshal([]byte(yamlString), m) -} diff --git a/internal/dagconfigservice/dagConfigService_test.go b/internal/dagconfigservice/dagConfigService_test.go deleted file mode 100644 index 0af5b11..0000000 --- a/internal/dagconfigservice/dagConfigService_test.go +++ /dev/null @@ -1,137 +0,0 @@ -package dagconfigservice - -import ( - "github.com/microsoft/abstrakt/internal/tools/guid" - "reflect" - "testing" -) - -func TestRelationshipFinding(t *testing.T) { - dag := &DagConfigService{} - _ = dag.LoadDagConfigFromString(test01DagStr) - rel1 := dag.FindRelationshipByFromName("Event Generator") - rel2 := dag.FindRelationshipByToName("Azure Event Hub") - - if rel1[0].From != rel2[0].From || rel1[0].To != rel2[0].To { - t.Error("Relationships were not correctly resolved") - } - -} - -func TestNewDagConfigFromString(t *testing.T) { - type targs struct { - yamlString string - } - tests := []struct { - name string - args targs - wantRet *DagConfigService - wantErr bool - }{ - { // TEST START - name: "Test.01", - args: targs{yamlString: test01DagStr}, - wantRet: &test01WantDag, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dag := &DagConfigService{} - err := dag.LoadDagConfigFromString(tt.args.yamlString) - if (err != nil) != tt.wantErr { - t.Errorf("LoadDagConfigFromString() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(dag, tt.wantRet) { - t.Errorf("LoadDagConfigFromString() =\n%#v,\nWant:\n%#v\n", dag, tt.wantRet) - } - }) - } -} - -func TestMultipleInstanceInRelationships(t *testing.T) { - testDag := test01DagStr - testDag += `- Id: "Event Generator to Event Logger Link" - Description: "Event Hubs to Event Logger connection" - From: "Event Generator" - To: "Event Logger" - Properties: {}` - - dag := &DagConfigService{} - _ = dag.LoadDagConfigFromString(testDag) - - from := dag.FindRelationshipByFromName("Event Generator") - to := dag.FindRelationshipByToName("Event Logger") - - if len(from) != 2 { - t.Error("Event Generator did not have the correct number of `From` relationships") - } - - if len(to) != 2 { - t.Error("Event Logger did not have the correct number of `To` relationships") - } -} - -// Sample DAG file data -const test01DagStr = `Name: "Azure Event Hubs Sample" -Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" -Services: -- Id: "Event Generator" - Type: "EventGenerator" - Properties: {} -- Id: "Azure Event Hub" - Type: "EventHub" - Properties: {} -- Id: "Event Logger" - Type: "EventLogger" - Properties: {} -Relationships: -- Id: "Generator to Event Hubs Link" - Description: "Event Generator to Event Hub connection" - From: "Event Generator" - To: "Azure Event Hub" - Properties: {} -- Id: "Event Hubs to Event Logger Link" - Description: "Event Hubs to Event Logger connection" - From: "Azure Event Hub" - To: "Event Logger" - Properties: {} -` - -var test01WantDag DagConfigService = DagConfigService{ - Name: "Azure Event Hubs Sample", - ID: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), - Services: []DagService{ - { - ID: "Event Generator", - Type: "EventGenerator", - Properties: make(map[string]DagProperty), - }, - { - ID: "Azure Event Hub", - Type: "EventHub", - Properties: make(map[string]DagProperty), - }, - { - ID: "Event Logger", - Type: "EventLogger", - Properties: make(map[string]DagProperty), - }, - }, - Relationships: []DagRelationship{ - { - ID: "Generator to Event Hubs Link", - Description: "Event Generator to Event Hub connection", - From: "Event Generator", - To: "Azure Event Hub", - Properties: make(map[string]DagProperty), - }, - { - ID: "Event Hubs to Event Logger Link", - Description: "Event Hubs to Event Logger connection", - From: "Azure Event Hub", - To: "Event Logger", - Properties: make(map[string]DagProperty), - }, - }, -} diff --git a/internal/chartservice/chartservice.go b/internal/platform/chart/chart.go similarity index 69% rename from internal/chartservice/chartservice.go rename to internal/platform/chart/chart.go index a0e4aae..da02255 100644 --- a/internal/chartservice/chartservice.go +++ b/internal/platform/chart/chart.go @@ -1,4 +1,4 @@ -package chartservice +package chart import ( "bytes" @@ -17,8 +17,8 @@ description: A Helm chart for Kubernetes version: 4.3.2 home: ""` -//CreateChart makes a new chart at the specified location -func CreateChart(name string, dir string) (chartReturn *chart.Chart, err error) { +//Create makes a new chart at the specified location +func Create(name string, dir string) (chartReturn *chart.Chart, err error) { tdir, err := ioutil.TempDir("./", "output-") if err != nil { @@ -77,8 +77,8 @@ func CreateChart(name string, dir string) (chartReturn *chart.Chart, err error) return } -// LoadChartFromDir loads a Helm chart from the specified director -func LoadChartFromDir(dir string) (*chart.Chart, error) { +// LoadFromDir loads a Helm chart from the specified director +func LoadFromDir(dir string) (*chart.Chart, error) { h, err := loader.LoadDir(dir) if err != nil { @@ -88,18 +88,18 @@ func LoadChartFromDir(dir string) (*chart.Chart, error) { return h, nil } -// SaveChartToDir takes the chart object and saves it as a set of files in the specified director -func SaveChartToDir(chart *chart.Chart, dir string) error { +// SaveToDir takes the chart object and saves it as a set of files in the specified director +func SaveToDir(chart *chart.Chart, dir string) error { return chartutil.SaveDir(chart, dir) } -// ZipChartToDir compresses the chart and saves it in compiled format -func ZipChartToDir(chart *chart.Chart, dir string) (string, error) { +// ZipToDir compresses the chart and saves it in compiled format +func ZipToDir(chart *chart.Chart, dir string) (string, error) { return chartutil.Save(chart, dir) } -// BuildChart download charts -func BuildChart(dir string) (out *bytes.Buffer, err error) { +// Build download charts +func Build(dir string) (out *bytes.Buffer, err error) { out = &bytes.Buffer{} diff --git a/internal/chartservice/chartservice_test.go b/internal/platform/chart/chart_test.go similarity index 93% rename from internal/chartservice/chartservice_test.go rename to internal/platform/chart/chart_test.go index f391a30..ba78835 100644 --- a/internal/chartservice/chartservice_test.go +++ b/internal/platform/chart/chart_test.go @@ -1,4 +1,4 @@ -package chartservice +package chart_test import ( "archive/tar" @@ -6,6 +6,7 @@ import ( "bytes" "compress/gzip" "flag" + "github.com/microsoft/abstrakt/internal/platform/chart" "io" "io/ioutil" "os" @@ -83,25 +84,24 @@ func TestChartSavesAndLoads(t *testing.T) { }() - c, err := CreateChart("foo", tdir) + c, err := chart.Create("foo", tdir) if err != nil { t.Fatal(err) } - err = SaveChartToDir(c, tdir2) + err = chart.SaveToDir(c, tdir2) if err != nil { t.Fatalf("Failed to save newly created chart %q: %s", tdir2, err) } newPath := filepath.Join(tdir2, "foo") - _, err = LoadChartFromDir(newPath) + _, err = chart.LoadFromDir(newPath) if err != nil { t.Fatalf("Failed to load newly created chart %q: %s", newPath, err) } - } func TestChartBuildChart(t *testing.T) { @@ -127,7 +127,7 @@ func TestChartBuildChart(t *testing.T) { t.Fatal(err) } - _, err = BuildChart(tdir + "/helm") + _, err = chart.Build(tdir + "/helm") if err != nil { t.Fatalf("Failed to BuildChart(): %s", err) } @@ -151,12 +151,12 @@ func TestZipChartToDir(t *testing.T) { } }() - chart, err := LoadChartFromDir("testdata/sample/helm") + helm, err := chart.LoadFromDir("testdata/sample/helm") if err != nil { t.Fatalf("Failed on LoadChartFromDir(): %s", err) } - _, err = ZipChartToDir(chart, tdir) + _, err = chart.ZipToDir(helm, tdir) if err != nil { t.Fatalf("Failed on ZipChartToDir(): %s", err) } diff --git a/internal/chartservice/testdata/golden/helm/Chart.yaml b/internal/platform/chart/testdata/golden/helm/Chart.yaml similarity index 100% rename from internal/chartservice/testdata/golden/helm/Chart.yaml rename to internal/platform/chart/testdata/golden/helm/Chart.yaml diff --git a/internal/chartservice/testdata/golden/helm/charts/event_hub_sample_event_generator-1.0.0.tgz b/internal/platform/chart/testdata/golden/helm/charts/event_hub_sample_event_generator-1.0.0.tgz similarity index 100% rename from internal/chartservice/testdata/golden/helm/charts/event_hub_sample_event_generator-1.0.0.tgz rename to internal/platform/chart/testdata/golden/helm/charts/event_hub_sample_event_generator-1.0.0.tgz diff --git a/internal/chartservice/testdata/golden/helm/charts/event_hub_sample_event_hub-1.0.0.tgz b/internal/platform/chart/testdata/golden/helm/charts/event_hub_sample_event_hub-1.0.0.tgz similarity index 100% rename from internal/chartservice/testdata/golden/helm/charts/event_hub_sample_event_hub-1.0.0.tgz rename to internal/platform/chart/testdata/golden/helm/charts/event_hub_sample_event_hub-1.0.0.tgz diff --git a/internal/chartservice/testdata/golden/helm/charts/event_hub_sample_event_logger-1.0.0.tgz b/internal/platform/chart/testdata/golden/helm/charts/event_hub_sample_event_logger-1.0.0.tgz similarity index 100% rename from internal/chartservice/testdata/golden/helm/charts/event_hub_sample_event_logger-1.0.0.tgz rename to internal/platform/chart/testdata/golden/helm/charts/event_hub_sample_event_logger-1.0.0.tgz diff --git a/internal/chartservice/testdata/golden/helm/templates/NOTES.txt b/internal/platform/chart/testdata/golden/helm/templates/NOTES.txt similarity index 100% rename from internal/chartservice/testdata/golden/helm/templates/NOTES.txt rename to internal/platform/chart/testdata/golden/helm/templates/NOTES.txt diff --git a/internal/chartservice/testdata/golden/helm/templates/_helpers.tpl b/internal/platform/chart/testdata/golden/helm/templates/_helpers.tpl similarity index 100% rename from internal/chartservice/testdata/golden/helm/templates/_helpers.tpl rename to internal/platform/chart/testdata/golden/helm/templates/_helpers.tpl diff --git a/internal/chartservice/testdata/golden/helm/templates/deployment.yaml b/internal/platform/chart/testdata/golden/helm/templates/deployment.yaml similarity index 100% rename from internal/chartservice/testdata/golden/helm/templates/deployment.yaml rename to internal/platform/chart/testdata/golden/helm/templates/deployment.yaml diff --git a/internal/chartservice/testdata/golden/helm/templates/ingress.yaml b/internal/platform/chart/testdata/golden/helm/templates/ingress.yaml similarity index 100% rename from internal/chartservice/testdata/golden/helm/templates/ingress.yaml rename to internal/platform/chart/testdata/golden/helm/templates/ingress.yaml diff --git a/internal/chartservice/testdata/golden/helm/templates/service.yaml b/internal/platform/chart/testdata/golden/helm/templates/service.yaml similarity index 100% rename from internal/chartservice/testdata/golden/helm/templates/service.yaml rename to internal/platform/chart/testdata/golden/helm/templates/service.yaml diff --git a/internal/chartservice/testdata/golden/helm/templates/serviceaccount.yaml b/internal/platform/chart/testdata/golden/helm/templates/serviceaccount.yaml similarity index 100% rename from internal/chartservice/testdata/golden/helm/templates/serviceaccount.yaml rename to internal/platform/chart/testdata/golden/helm/templates/serviceaccount.yaml diff --git a/internal/chartservice/testdata/golden/helm/templates/tests/test-connection.yaml b/internal/platform/chart/testdata/golden/helm/templates/tests/test-connection.yaml similarity index 100% rename from internal/chartservice/testdata/golden/helm/templates/tests/test-connection.yaml rename to internal/platform/chart/testdata/golden/helm/templates/tests/test-connection.yaml diff --git a/internal/chartservice/testdata/golden/helm/values.yaml b/internal/platform/chart/testdata/golden/helm/values.yaml similarity index 100% rename from internal/chartservice/testdata/golden/helm/values.yaml rename to internal/platform/chart/testdata/golden/helm/values.yaml diff --git a/internal/chartservice/testdata/golden/test-0.1.0.tgz b/internal/platform/chart/testdata/golden/test-0.1.0.tgz similarity index 100% rename from internal/chartservice/testdata/golden/test-0.1.0.tgz rename to internal/platform/chart/testdata/golden/test-0.1.0.tgz diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/.helmignore b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/.helmignore similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/.helmignore rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/.helmignore diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/Chart.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/Chart.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/Chart.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/Chart.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/NOTES.txt b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/NOTES.txt similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/NOTES.txt rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/NOTES.txt diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/_helpers.tpl b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/_helpers.tpl similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/_helpers.tpl rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/_helpers.tpl diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/deployment.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/deployment.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/deployment.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/deployment.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/ingress.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/ingress.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/ingress.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/ingress.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/service.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/service.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/service.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/service.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/values.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/values.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_generator/values.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_generator/values.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/.helmignore b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/.helmignore similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/.helmignore rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/.helmignore diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/Chart.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/Chart.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/Chart.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/Chart.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/NOTES.txt b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/NOTES.txt similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/NOTES.txt rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/NOTES.txt diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/_helpers.tpl b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/_helpers.tpl similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/_helpers.tpl rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/_helpers.tpl diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/deployment.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/deployment.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/deployment.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/deployment.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/ingress.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/ingress.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/ingress.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/ingress.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/service.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/service.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/service.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/service.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/values.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/values.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_hub/values.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_hub/values.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/.helmignore b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/.helmignore similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/.helmignore rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/.helmignore diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/Chart.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/Chart.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/Chart.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/Chart.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/NOTES.txt b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/NOTES.txt similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/NOTES.txt rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/NOTES.txt diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/_helpers.tpl b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/_helpers.tpl similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/_helpers.tpl rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/_helpers.tpl diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/deployment.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/deployment.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/deployment.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/deployment.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/ingress.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/ingress.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/ingress.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/ingress.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/service.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/service.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/service.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/service.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml diff --git a/internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/values.yaml b/internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/values.yaml similarity index 100% rename from internal/chartservice/testdata/sample/deps/event_hub_sample_event_logger/values.yaml rename to internal/platform/chart/testdata/sample/deps/event_hub_sample_event_logger/values.yaml diff --git a/internal/chartservice/testdata/sample/helm/Chart.yaml b/internal/platform/chart/testdata/sample/helm/Chart.yaml similarity index 100% rename from internal/chartservice/testdata/sample/helm/Chart.yaml rename to internal/platform/chart/testdata/sample/helm/Chart.yaml diff --git a/internal/chartservice/testdata/sample/helm/templates/NOTES.txt b/internal/platform/chart/testdata/sample/helm/templates/NOTES.txt similarity index 100% rename from internal/chartservice/testdata/sample/helm/templates/NOTES.txt rename to internal/platform/chart/testdata/sample/helm/templates/NOTES.txt diff --git a/internal/chartservice/testdata/sample/helm/templates/_helpers.tpl b/internal/platform/chart/testdata/sample/helm/templates/_helpers.tpl similarity index 100% rename from internal/chartservice/testdata/sample/helm/templates/_helpers.tpl rename to internal/platform/chart/testdata/sample/helm/templates/_helpers.tpl diff --git a/internal/chartservice/testdata/sample/helm/templates/deployment.yaml b/internal/platform/chart/testdata/sample/helm/templates/deployment.yaml similarity index 100% rename from internal/chartservice/testdata/sample/helm/templates/deployment.yaml rename to internal/platform/chart/testdata/sample/helm/templates/deployment.yaml diff --git a/internal/chartservice/testdata/sample/helm/templates/ingress.yaml b/internal/platform/chart/testdata/sample/helm/templates/ingress.yaml similarity index 100% rename from internal/chartservice/testdata/sample/helm/templates/ingress.yaml rename to internal/platform/chart/testdata/sample/helm/templates/ingress.yaml diff --git a/internal/chartservice/testdata/sample/helm/templates/service.yaml b/internal/platform/chart/testdata/sample/helm/templates/service.yaml similarity index 100% rename from internal/chartservice/testdata/sample/helm/templates/service.yaml rename to internal/platform/chart/testdata/sample/helm/templates/service.yaml diff --git a/internal/chartservice/testdata/sample/helm/templates/serviceaccount.yaml b/internal/platform/chart/testdata/sample/helm/templates/serviceaccount.yaml similarity index 100% rename from internal/chartservice/testdata/sample/helm/templates/serviceaccount.yaml rename to internal/platform/chart/testdata/sample/helm/templates/serviceaccount.yaml diff --git a/internal/chartservice/testdata/sample/helm/templates/tests/test-connection.yaml b/internal/platform/chart/testdata/sample/helm/templates/tests/test-connection.yaml similarity index 100% rename from internal/chartservice/testdata/sample/helm/templates/tests/test-connection.yaml rename to internal/platform/chart/testdata/sample/helm/templates/tests/test-connection.yaml diff --git a/internal/chartservice/testdata/sample/helm/values.yaml b/internal/platform/chart/testdata/sample/helm/values.yaml similarity index 100% rename from internal/chartservice/testdata/sample/helm/values.yaml rename to internal/platform/chart/testdata/sample/helm/values.yaml diff --git a/internal/platform/constellation/constellation.go b/internal/platform/constellation/constellation.go new file mode 100644 index 0000000..52a08e3 --- /dev/null +++ b/internal/platform/constellation/constellation.go @@ -0,0 +1,71 @@ +package constellation + +//////////////////////////////////////////////////////////// +// DagConfig class - information for a deployment regarding +// the Services and the Relationships between them. +// +// Usual starting point would be to construct a DatConfig +// instance from the corresponding yaml using either: +// dcPointer := NewDagConfigFromFile() +// or +// dcPointer := NewDagConfigFromString() +// +// Parsing failures are indicated by a nil return. +//////////////////////////////////////////////////////////// + +import ( + "github.com/microsoft/abstrakt/internal/tools/guid" + yamlParser "gopkg.in/yaml.v2" + "io/ioutil" +) + +// Note: the yaml mappings are necessary (despite the 1-1 name correspondence). +// The yaml parser would otherwise expect the names in the YAML file to be all +// lower-case. e.g. ChartName would only work if "chartname" was used in the +// yaml file. + +// Property - an individual property in the DAG. +// For now, these are just interfaces as the value types are not firmed up +// for individual properties. As the entire set of properties becomes +// known, each should be promoted out of the Properties collection to +// the main struct -- handling presence/absence via using pointer members, +// so as to allow for nil value == absence. +type Property interface{} + +// Service -- a DAG Service description +type Service struct { + ID string `yaml:"Id" validate:"empty=false"` + Type string `yaml:"Type" validate:"empty=false"` + Properties map[string]Property `yaml:"Properties"` +} + +// Relationship -- a relationship between Services +type Relationship struct { + ID string `yaml:"Id" validate:"empty=false"` + Description string `yaml:"Description"` + From string `yaml:"From" validate:"empty=false"` + To string `yaml:"To" validate:"empty=false"` + Properties map[string]Property `yaml:"Properties"` +} + +// Config -- The DAG config for a deployment +type Config struct { + Name string `yaml:"Name" validate:"empty=false"` + ID guid.GUID `yaml:"Id" validate:"empty=false"` + Services []Service `yaml:"Services" validate:"empty=false"` + Relationships []Relationship `yaml:"Relationships"` +} + +// LoadFile -- New DAG info instance from the named file. +func (m *Config) LoadFile(fileName string) (err error) { + contentBytes, err := ioutil.ReadFile(fileName) + if nil != err { + return + } + return m.LoadString(string(contentBytes)) +} + +// LoadString -- New DAG info instance from the given yaml string. +func (m *Config) LoadString(yamlString string) (err error) { + return yamlParser.Unmarshal([]byte(yamlString), m) +} diff --git a/internal/platform/constellation/constellation_test.go b/internal/platform/constellation/constellation_test.go new file mode 100644 index 0000000..bb26031 --- /dev/null +++ b/internal/platform/constellation/constellation_test.go @@ -0,0 +1,83 @@ +package constellation_test + +import ( + "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/tools/guid" + "io/ioutil" + "reflect" + "testing" +) + +func TestNewDagConfigFromString(t *testing.T) { + contentBytes, err := ioutil.ReadFile("testdata/valid.yaml") + if nil != err { + t.Fatal(err) + } + + type targs struct { + yamlString string + } + tests := []struct { + name string + args targs + wantRet *constellation.Config + wantErr bool + }{ + { // TEST START + name: "Test.01", + args: targs{yamlString: string(contentBytes)}, + wantRet: &test01WantDag, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dag := &constellation.Config{} + err := dag.LoadString(tt.args.yamlString) + if (err != nil) != tt.wantErr { + t.Errorf("LoadDagConfigFromString() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(dag, tt.wantRet) { + t.Errorf("LoadDagConfigFromString() =\n%#v,\nWant:\n%#v\n", dag, tt.wantRet) + } + }) + } +} + +var test01WantDag constellation.Config = constellation.Config{ + Name: "Azure Event Hubs Sample", + ID: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), + Services: []constellation.Service{ + { + ID: "Event Generator", + Type: "EventGenerator", + Properties: make(map[string]constellation.Property), + }, + { + ID: "Azure Event Hub", + Type: "EventHub", + Properties: make(map[string]constellation.Property), + }, + { + ID: "Event Logger", + Type: "EventLogger", + Properties: make(map[string]constellation.Property), + }, + }, + Relationships: []constellation.Relationship{ + { + ID: "Generator to Event Hubs Link", + Description: "Event Generator to Event Hub connection", + From: "Event Generator", + To: "Azure Event Hub", + Properties: make(map[string]constellation.Property), + }, + { + ID: "Event Hubs to Event Logger Link", + Description: "Event Hubs to Event Logger connection", + From: "Azure Event Hub", + To: "Event Logger", + Properties: make(map[string]constellation.Property), + }, + }, +} diff --git a/internal/platform/constellation/find.go b/internal/platform/constellation/find.go new file mode 100644 index 0000000..0fc9dab --- /dev/null +++ b/internal/platform/constellation/find.go @@ -0,0 +1,57 @@ +package constellation + +import ( + "github.com/microsoft/abstrakt/internal/tools/guid" + "strings" +) + +// FindService -- Find a Service by id. +func (m *Config) FindService(serviceID string) *Service { + for _, val := range m.Services { + if val.ID == serviceID { + return &val + } + if guid.TolerateMiscasedKey && strings.EqualFold(val.ID, serviceID) { + return &val + } + } + return nil +} + +// FindRelationship -- Find a Relationship by id. +func (m *Config) FindRelationship(relationshipID string) *Relationship { + for _, val := range m.Relationships { + if val.ID == relationshipID { + return &val + } else if guid.TolerateMiscasedKey && strings.EqualFold(val.ID, relationshipID) { + return &val + } + } + return nil +} + +// FindRelationshipByToName -- Find a Relationship by the name that is the target of the rel. +func (m *Config) FindRelationshipByToName(relationshipToName string) (res []Relationship) { + for _, val := range m.Relationships { + // try first for an exact match + if val.To == relationshipToName { + res = append(res, val) + } else if guid.TolerateMiscasedKey && strings.EqualFold(string(val.To), relationshipToName) { + res = append(res, val) + } + } + return +} + +// FindRelationshipByFromName -- Find a Relationship by the name that is the source of the rel. +func (m *Config) FindRelationshipByFromName(relationshipFromName string) (res []Relationship) { + for _, val := range m.Relationships { + // try first for an exact match + if val.From == relationshipFromName { + res = append(res, val) + } else if guid.TolerateMiscasedKey && strings.EqualFold(string(val.From), relationshipFromName) { + res = append(res, val) + } + } + return +} diff --git a/internal/platform/constellation/find_test.go b/internal/platform/constellation/find_test.go new file mode 100644 index 0000000..f3289fa --- /dev/null +++ b/internal/platform/constellation/find_test.go @@ -0,0 +1,42 @@ +package constellation_test + +import ( + "github.com/microsoft/abstrakt/internal/platform/constellation" + "testing" +) + +func TestRelationshipFinding(t *testing.T) { + dag := new(constellation.Config) + _ = dag.LoadFile("testdata/valid.yaml") + rel1 := dag.FindRelationshipByFromName("Event Generator") + rel2 := dag.FindRelationshipByToName("Azure Event Hub") + + if rel1[0].From != rel2[0].From || rel1[0].To != rel2[0].To { + t.Error("Relationships were not correctly resolved") + } +} + +func TestMultipleInstanceInRelationships(t *testing.T) { + newRelationship := constellation.Relationship{ + ID: "Event Generator to Event Logger Link", + Description: "Event Hubs to Event Logger connection", + From: "Event Generator", + To: "Event Logger", + } + + dag := new(constellation.Config) + _ = dag.LoadFile("testdata/valid.yaml") + + dag.Relationships = append(dag.Relationships, newRelationship) + + from := dag.FindRelationshipByFromName("Event Generator") + to := dag.FindRelationshipByToName("Event Logger") + + if len(from) != 2 { + t.Error("Event Generator did not have the correct number of `From` relationships") + } + + if len(to) != 2 { + t.Error("Event Logger did not have the correct number of `To` relationships") + } +} diff --git a/internal/platform/constellation/graph.go b/internal/platform/constellation/graph.go new file mode 100644 index 0000000..5bad085 --- /dev/null +++ b/internal/platform/constellation/graph.go @@ -0,0 +1,64 @@ +package constellation + +import ( + "fmt" + "github.com/awalterschulze/gographviz" + "strings" + "io" +) + +// GenerateGraph - function to take a dagconfigService structure and create a graph object that contains the +// representation of the graph. Also outputs a string representation (GraphViz dot notation) of the resulting graph +// this can be passed on to GraphViz to graphically render the resulting graph +func (readGraph *Config) GenerateGraph(out io.Writer) (string, error) { + + // Lookup is used to map IDs to names. Names are easier to visualise but IDs are more important to ensure the + // presented constellation is correct and IDs are used to link nodes together + lookup := make(map[string]string) + + g := gographviz.NewGraph() + + // Replace spaces with underscores, names with spaces can break graphviz engines + if err := g.SetName(strings.Replace(readGraph.Name, " ", "_", -1)); err != nil { + return "", fmt.Errorf("error: %v", err) + } + if err := g.AddAttr(g.Name, "rankdir", "LR"); err != nil { + return "", fmt.Errorf("error adding attribute: %v", err) + } + + // Make the graph directed (a constellation is DAG) + if err := g.SetDir(true); err != nil { + return "", fmt.Errorf("error: %v", err) + } + + // Add all nodes to the graph storing the lookup from ID to name (for later adding relationships) + // Replace spaces in names with underscores, names with spaces can break graphviz engines) + for _, v := range readGraph.Services { + fmt.Fprintf(out, "Adding node %s\n", v.ID) + newName := strings.Replace(v.ID, " ", "_", -1) + + if strings.Compare(newName, v.ID) != 0 { + fmt.Fprintf(out, "Changing %s to %s\n", v.ID, newName) + } + lookup[v.ID] = newName + err := g.AddNode(readGraph.Name, "\""+newName+"\"", nil) + if err != nil { + return "", fmt.Errorf("error: %v", err) + } + } + + // Add relationships to the graph linking using the lookup IDs to name map + // Replace spaces in names with underscores, names with spaces can break graphviz engines) + for _, v := range readGraph.Relationships { + fmt.Fprintf(out, "Adding relationship from %s ---> %s\n", v.From, v.To) + localFrom := "\"" + lookup[v.From] + "\"" + localTo := "\"" + lookup[v.To] + "\"" + err := g.AddEdge(localFrom, localTo, true, nil) + if err != nil { + return "", fmt.Errorf("error: %v", err) + } + } + + // Produce resulting graph in dot notation format + return g.String(), nil +} diff --git a/internal/platform/constellation/graph_test.go b/internal/platform/constellation/graph_test.go new file mode 100644 index 0000000..8c38ca8 --- /dev/null +++ b/internal/platform/constellation/graph_test.go @@ -0,0 +1,42 @@ +package constellation_test + +import ( + "bytes" + "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/tools/helpers" + "testing" +) + +func TestGenerateGraph(t *testing.T) { + + retConfig := new(constellation.Config) + err := retConfig.LoadFile("testdata/valid.yaml") + if err != nil { + panic(err) + } + + out := &bytes.Buffer{} + + cmpString := test02ConstGraphString + retString, err := retConfig.GenerateGraph(out) + + if err != nil { + t.Errorf("Should not recieve error: %v", err) + } + + if !helpers.CompareGraphOutputAsSets(cmpString, retString) { + t.Errorf("Input graph did not generate expected output graphviz representation") + t.Errorf("Expected:\n%v \nGot:\n%v", cmpString, retString) + } +} + +const test02ConstGraphString = `digraph Azure_Event_Hubs_Sample { + rankdir=LR; + "Event_Generator"->"Azure_Event_Hub"; + "Azure_Event_Hub"->"Event_Logger"; + "Azure_Event_Hub"; + "Event_Generator"; + "Event_Logger"; + +} +` diff --git a/internal/validationservice/testdata/duplicate/relIds.yaml b/internal/platform/constellation/testdata/duplicate/relIds.yaml similarity index 100% rename from internal/validationservice/testdata/duplicate/relIds.yaml rename to internal/platform/constellation/testdata/duplicate/relIds.yaml diff --git a/internal/validationservice/testdata/duplicate/servIds.yaml b/internal/platform/constellation/testdata/duplicate/servIds.yaml similarity index 100% rename from internal/validationservice/testdata/duplicate/servIds.yaml rename to internal/platform/constellation/testdata/duplicate/servIds.yaml diff --git a/internal/validationservice/testdata/duplicate/servRelIds.yaml b/internal/platform/constellation/testdata/duplicate/servRelIds.yaml similarity index 100% rename from internal/validationservice/testdata/duplicate/servRelIds.yaml rename to internal/platform/constellation/testdata/duplicate/servRelIds.yaml diff --git a/internal/validationservice/testdata/missing/id.yaml b/internal/platform/constellation/testdata/missing/id.yaml similarity index 100% rename from internal/validationservice/testdata/missing/id.yaml rename to internal/platform/constellation/testdata/missing/id.yaml diff --git a/internal/validationservice/testdata/missing/name.yaml b/internal/platform/constellation/testdata/missing/name.yaml similarity index 100% rename from internal/validationservice/testdata/missing/name.yaml rename to internal/platform/constellation/testdata/missing/name.yaml diff --git a/internal/validationservice/testdata/missing/relId.yaml b/internal/platform/constellation/testdata/missing/relId.yaml similarity index 100% rename from internal/validationservice/testdata/missing/relId.yaml rename to internal/platform/constellation/testdata/missing/relId.yaml diff --git a/internal/validationservice/testdata/missing/relServRefId.yaml b/internal/platform/constellation/testdata/missing/relServRefId.yaml similarity index 100% rename from internal/validationservice/testdata/missing/relServRefId.yaml rename to internal/platform/constellation/testdata/missing/relServRefId.yaml diff --git a/internal/validationservice/testdata/missing/serv.yaml b/internal/platform/constellation/testdata/missing/serv.yaml similarity index 100% rename from internal/validationservice/testdata/missing/serv.yaml rename to internal/platform/constellation/testdata/missing/serv.yaml diff --git a/internal/validationservice/testdata/missing/servId.yaml b/internal/platform/constellation/testdata/missing/servId.yaml similarity index 100% rename from internal/validationservice/testdata/missing/servId.yaml rename to internal/platform/constellation/testdata/missing/servId.yaml diff --git a/internal/validationservice/testdata/valid.yaml b/internal/platform/constellation/testdata/valid.yaml similarity index 100% rename from internal/validationservice/testdata/valid.yaml rename to internal/platform/constellation/testdata/valid.yaml diff --git a/internal/platform/constellation/validate.go b/internal/platform/constellation/validate.go new file mode 100644 index 0000000..e8d1853 --- /dev/null +++ b/internal/platform/constellation/validate.go @@ -0,0 +1,68 @@ +package constellation + +import "gopkg.in/dealancer/validate.v2" + +// CheckDuplicates checks for duplicate Relationship and Service IDs in a constellation file. +func (m *Config) CheckDuplicates() (duplicates []string) { + IDs := []string{string(m.ID)} + + for _, i := range m.Services { + _, exists := find(IDs, i.ID) + if exists { + duplicates = append(duplicates, i.ID) + } else { + IDs = append(IDs, i.ID) + } + } + + for _, i := range m.Relationships { + _, exists := find(IDs, i.ID) + if exists { + duplicates = append(duplicates, i.ID) + } else { + IDs = append(IDs, i.ID) + } + } + + return +} + +// CheckServiceExists loops through each Relationship and checks if the services are declared. +func (m *Config) CheckServiceExists() (missing map[string][]string) { + missing = make(map[string][]string) + IDs := []string{} + + for _, i := range m.Services { + IDs = append(IDs, i.ID) + } + + for _, i := range m.Relationships { + _, exists := find(IDs, i.To) + if !exists { + missing[i.ID] = append(missing[i.ID], i.To) + } + + _, exists = find(IDs, i.From) + if !exists { + missing[i.ID] = append(missing[i.ID], i.From) + } + } + + return +} + +// ValidateModel checks if constellation has all required felids +func (m *Config) ValidateModel() error { + return validate.Validate(m) +} + +// find takes a slice and looks for an element in it. If found it will +// return it's key, otherwise it will return -1 and a bool of false. +func find(slice []string, val string) (int, bool) { + for i, item := range slice { + if item == val { + return i, true + } + } + return -1, false +} diff --git a/internal/platform/constellation/validate_test.go b/internal/platform/constellation/validate_test.go new file mode 100644 index 0000000..77cb27a --- /dev/null +++ b/internal/platform/constellation/validate_test.go @@ -0,0 +1,143 @@ +package constellation_test + +import ( + "github.com/microsoft/abstrakt/internal/platform/constellation" + "testing" +) + +func TestForDuplicatIDsInServices(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/valid.yaml") + + duplicates := testData.CheckDuplicates() + + if duplicates != nil { + t.Error("No duplicates should be found.") + } +} + +func TestServicesExists(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/valid.yaml") + + missing := testData.CheckServiceExists() + + if len(missing) != 0 { + t.Error("No missing services should be found.") + } +} + +func TestSchemaChecks(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/valid.yaml") + + err := testData.ValidateModel() + + if err != nil { + t.Errorf("Model validation should not return errors: %v", err.Error()) + } +} + +func TestForDuplicatIDsInServicesFail(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/duplicate/servIds.yaml") + + duplicates := testData.CheckDuplicates() + + if duplicates == nil { + t.Errorf("There should be %v duplicate IDs found", len(testData.Services)) + } +} + +func TestForDuplicatIDsInRelationshipsFail(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/duplicate/relIds.yaml") + + duplicates := testData.CheckDuplicates() + + if duplicates == nil { + t.Errorf("There should be %v duplicate IDs found", len(testData.Relationships)) + } +} + +func TestForDuplicatIDsFail(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/duplicate/servRelIds.yaml") + + duplicates := testData.CheckDuplicates() + + if duplicates == nil { + t.Error("There should be 2 duplicate IDs found") + } +} + +func TestServicesExistsFail(t *testing.T) { + expected := "Azure Event Hub" + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/missing/relServRefId.yaml") + + missing := testData.CheckServiceExists() + + foundID := missing["Event Hubs to Event Logger Link"][0] + + if len(missing) != 1 { + t.Error("There should be only 1 missing services found") + } else if foundID != expected { + t.Errorf("Incorrect reference found. Expected: %v \nGot: %v", expected, foundID) + } +} + +func TestSchemaMissingDagName(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/missingName.yaml") + + err := testData.ValidateModel() + + if err == nil { + t.Error("Model validation should be invalid") + } +} + +func TestSchemaMissingDagID(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/missing/id.yaml") + + err := testData.ValidateModel() + + if err == nil { + t.Error("Model validation should be invalid") + } +} + +func TestSchemaMissingService(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/missing/serv.yaml") + + err := testData.ValidateModel() + + if err == nil { + t.Error("Model validation should be invalid") + } +} + +func TestSchemaMissingServiceID(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/missing/servId.yaml") + + err := testData.ValidateModel() + + if err == nil { + t.Error("Model validation should be invalid") + } +} + +func TestSchemaMissingRelationshipID(t *testing.T) { + testData := new(constellation.Config) + _ = testData.LoadFile("testdata/missing/relId.yaml") + + err := testData.ValidateModel() + + if err == nil { + t.Error("Model validation should be invalid") + } +} diff --git a/internal/platform/mapper/find.go b/internal/platform/mapper/find.go new file mode 100644 index 0000000..377f528 --- /dev/null +++ b/internal/platform/mapper/find.go @@ -0,0 +1,32 @@ +package mapper + +import ( + "github.com/microsoft/abstrakt/internal/tools/guid" + "strings" +) + +// FindByName -- Look up a map by chart name. +func (m *Config) FindByName(chartName string) *Info { + for _, wmi := range m.Maps { + if chartName == wmi.ChartName { + return &wmi + } + if guid.TolerateMiscasedKey && strings.EqualFold(string(wmi.ChartName), chartName) { + return &wmi + } + } + return nil +} + +// FindByType -- Look up a map by the "Type" value. +func (m *Config) FindByType(typeName string) *Info { + for _, wmi := range m.Maps { + if typeName == wmi.Type { + return &wmi + } + if guid.TolerateMiscasedKey && strings.EqualFold(string(wmi.Type), typeName) { + return &wmi + } + } + return nil +} diff --git a/internal/platform/mapper/mapper.go b/internal/platform/mapper/mapper.go new file mode 100644 index 0000000..b3a6670 --- /dev/null +++ b/internal/platform/mapper/mapper.go @@ -0,0 +1,47 @@ +package mapper + +////////////////////////////////////////////////////// +// BuildMapService: Process map files relating services +// to helm chart files. For example, see accompanying +// test file. +////////////////////////////////////////////////////// + +import ( + "github.com/microsoft/abstrakt/internal/tools/guid" + yamlParser "gopkg.in/yaml.v2" + "io/ioutil" +) + +// Note: the yaml mapping attributes are necessary (despite the nearly +// uniform 1-1 name correspondence). The yaml parser would otherwise +// expect the names in the YAML file to be all lower-case. +// e.g. ChartName would only work if "chartname" was used in the yaml file. + +// Info -- info about an individual component +type Info struct { + ChartName string `yaml:"ChartName"` + Type string `yaml:"Type"` + Location string `yaml:"Location"` + Version string `yaml:"Version"` +} + +// Config -- data from the entire build map. +type Config struct { + Name string `yaml:"Name"` + ID guid.GUID `yaml:"Id"` + Maps []Info `yaml:"Maps"` +} + +// LoadFile -- New Map info instance from the named file. +func (m *Config) LoadFile(fileName string) (err error) { + contentBytes, err := ioutil.ReadFile(fileName) + if nil != err { + return + } + return m.LoadString(string(contentBytes)) +} + +// LoadString -- New Map info instance from the given yaml string. +func (m *Config) LoadString(yamlString string) error { + return yamlParser.Unmarshal([]byte(yamlString), m) +} diff --git a/internal/buildmapservice/buildmapservice_test.go b/internal/platform/mapper/mapper_test.go similarity index 87% rename from internal/buildmapservice/buildmapservice_test.go rename to internal/platform/mapper/mapper_test.go index eea6f28..719bb03 100644 --- a/internal/buildmapservice/buildmapservice_test.go +++ b/internal/platform/mapper/mapper_test.go @@ -1,10 +1,10 @@ -package buildmapservice +package mapper_test import ( + "github.com/microsoft/abstrakt/internal/platform/mapper" + "github.com/microsoft/abstrakt/internal/tools/guid" "reflect" "testing" - - "github.com/microsoft/abstrakt/internal/tools/guid" ) func TestMapFromString(t *testing.T) { @@ -14,7 +14,7 @@ func TestMapFromString(t *testing.T) { tests := []struct { name string args args - wantRet *BuildMapService + wantRet *mapper.Config wantErr bool }{ { @@ -26,8 +26,8 @@ func TestMapFromString(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - mapper := &BuildMapService{} - err := mapper.LoadMapFromString(tt.args.yamlString) + mapper := &mapper.Config{} + err := mapper.LoadString(tt.args.yamlString) if (err != nil) != tt.wantErr { t.Errorf("LoadMapFromString() error = %v, wantErr %v", err, tt.wantErr) return @@ -57,10 +57,10 @@ Maps: Version: "1.0.0" ` -var buildMap01 = BuildMapService{ +var buildMap01 = mapper.Config{ Name: "Basic Azure Event Hubs maps", ID: guid.GUID("a5a7c413-a020-44a2-bd23-1941adb7ad58"), - Maps: []BuildMapInfo{ + Maps: []mapper.Info{ { ChartName: "event_hub_sample_event_generator", Type: "EventGenerator", diff --git a/internal/tools/guid/guid.go b/internal/tools/guid/guid.go index 5407617..8a4aa7c 100644 --- a/internal/tools/guid/guid.go +++ b/internal/tools/guid/guid.go @@ -10,9 +10,7 @@ package guid // //////////////////////////////////////////////////////////// -import ( - "strings" -) +import "strings" // If true, tolerate Find requests where the case is incorrect. // e.g. if asked for Name="abc", then okay to return the object @@ -30,20 +28,20 @@ const TolerateMiscasedKey = true // GUID -- an alias for an ID type GUID string -// EmptyGUID -- equivalent to an uninitialized GUID. -const EmptyGUID = GUID("") +// Empty -- equivalent to an uninitialized GUID. +const Empty = GUID("") // IsEmpty - true if GUID represents empty value. func (LHS GUID) IsEmpty() bool { - return EmptyGUID.Equals(LHS) + return Empty.Equals(LHS) } // Equals -- true if two GUIDs compare equal. Case differences are tolerated. func (LHS GUID) Equals(RHS GUID) bool { - if LHS == EmptyGUID && RHS == EmptyGUID { + if LHS == Empty && RHS == Empty { return true } - if LHS == EmptyGUID || RHS == EmptyGUID { + if LHS == Empty || RHS == Empty { return false } diff --git a/internal/tools/guid/guid_test.go b/internal/tools/guid/guid_test.go new file mode 100644 index 0000000..a9bfddf --- /dev/null +++ b/internal/tools/guid/guid_test.go @@ -0,0 +1,68 @@ +package guid_test + +import ( + "github.com/microsoft/abstrakt/internal/tools/guid" + "testing" +) + +func TestGUID_Equals(t *testing.T) { + type args struct { + RHS guid.GUID + } + tests := []struct { + name string + LHS guid.GUID + args args + want bool + }{ + { + name: "GUID.01", // Exact match + LHS: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), + args: args{RHS: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5")}, + want: true, + }, + { + name: "GUID.02", // Differing case + LHS: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), + args: args{RHS: guid.GUID("D6E4a5e9-696a-4626-bA7a-534d6ff450a5")}, + want: true, + }, + { + name: "GUID.03", // Not equivalent + LHS: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), + args: args{RHS: guid.GUID("aaa4a5e9-696a-4626-ba7a-534d6ff450a5")}, + want: false, + }, + { + name: "GUID.04", + LHS: guid.Empty, + args: args{RHS: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5")}, + want: false, + }, + { + name: "GUID.05", + LHS: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), + args: args{RHS: guid.Empty}, + want: false, + }, + { + name: "GUID.06", + LHS: guid.Empty, + args: args{RHS: guid.Empty}, + want: true, + }, + { + name: "GUID.07", + LHS: guid.Empty, + args: args{RHS: guid.GUID("some junk")}, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.LHS.Equals(tt.args.RHS); got != tt.want { + t.Errorf("GUID.Equals() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/internal/tools/guid/guids_test.go b/internal/tools/guid/guids_test.go deleted file mode 100644 index 2cb8306..0000000 --- a/internal/tools/guid/guids_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package guid - -import "testing" - -func TestGUID_Equals(t *testing.T) { - type args struct { - RHS GUID - } - tests := []struct { - name string - LHS GUID - args args - want bool - }{ - { - name: "GUID.01", // Exact match - LHS: GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), - args: args{RHS: GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5")}, - want: true, - }, - { - name: "GUID.02", // Differing case - LHS: GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), - args: args{RHS: GUID("D6E4a5e9-696a-4626-bA7a-534d6ff450a5")}, - want: true, - }, - { - name: "GUID.03", // Not equivalent - LHS: GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), - args: args{RHS: GUID("aaa4a5e9-696a-4626-ba7a-534d6ff450a5")}, - want: false, - }, - { - name: "GUID.04", - LHS: EmptyGUID, - args: args{RHS: GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5")}, - want: false, - }, - { - name: "GUID.05", - LHS: GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), - args: args{RHS: EmptyGUID}, - want: false, - }, - { - name: "GUID.06", - LHS: EmptyGUID, - args: args{RHS: EmptyGUID}, - want: true, - }, - { - name: "GUID.07", - LHS: EmptyGUID, - args: args{RHS: GUID("some junk")}, - want: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := tt.LHS.Equals(tt.args.RHS); got != tt.want { - t.Errorf("GUID.Equals() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/internal/tools/helpers/file.go b/internal/tools/helpers/file.go new file mode 100644 index 0000000..da31129 --- /dev/null +++ b/internal/tools/helpers/file.go @@ -0,0 +1,12 @@ +package helpers + +import "os" + +// FileExists - basic utility function to check the provided filename can be opened and is not a folder/directory +func FileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} diff --git a/internal/tools/helpers/test.go b/internal/tools/helpers/test.go new file mode 100644 index 0000000..3e4fe09 --- /dev/null +++ b/internal/tools/helpers/test.go @@ -0,0 +1,81 @@ +package helpers + +import ( + "bytes" + "fmt" + set "github.com/deckarep/golang-set" + "github.com/spf13/cobra" + "io/ioutil" + "os" + "path" + "strings" + "testing" +) + +func ExecuteCommand(root *cobra.Command, args ...string) (output string, err error) { + _, output, err = executeCommandC(root, args...) + return output, err +} + +func executeCommandC(root *cobra.Command, args ...string) (c *cobra.Command, output string, err error) { + buf := new(bytes.Buffer) + root.SetOutput(buf) + root.SetArgs(args) + c, err = root.ExecuteC() + return +} + +func CheckStringContains(t *testing.T, got, expected string) { + if !strings.Contains(got, expected) { + t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got) + } +} + +func PrepareRealFilesForTest(t *testing.T) (string, string, string) { + tdir, err := ioutil.TempDir("./", "output-") + if err != nil { + t.Fatal(err) + } + + cwd, err2 := os.Getwd() + if err2 != nil { + t.Fatal(err2) + } + + fmt.Print(cwd) + + constellationPath := path.Join(cwd, "../sample/constellation/sample_constellation.yaml") + mapsPath := path.Join(cwd, "../sample/constellation/sample_constellation_maps.yaml") + + return constellationPath, mapsPath, tdir +} + +func CleanTempTestFiles(t *testing.T, temp string) { + err := os.RemoveAll(temp) + if err != nil { + t.Fatal(err) + } +} + +// CompareGraphOutputAsSets - the graphviz library does not always output the result string with nodes and edges +// in the same order (it can vary between calls). This does not impact using the result but makes testing the result a +// headache as the assumption is that the expected string and the produced string would match exactly. When the sequence +// changes they dont match. This function converts the strings into sets of lines and compares if the lines in the two outputs +// are the same +func CompareGraphOutputAsSets(expected, produced string) bool { + lstExpected := strings.Split(expected, "\n") + lstProduced := strings.Split(produced, "\n") + + setExpected := set.NewSet() + setProduced := set.NewSet() + + for l := range lstExpected { + setExpected.Add(l) + } + + for l := range lstProduced { + setProduced.Add(l) + } + + return setProduced.Equal(setExpected) +} diff --git a/internal/validationservice/validationservice.go b/internal/validationservice/validationservice.go deleted file mode 100644 index f97098b..0000000 --- a/internal/validationservice/validationservice.go +++ /dev/null @@ -1,76 +0,0 @@ -package validationservice - -import ( - "github.com/microsoft/abstrakt/internal/dagconfigservice" - "gopkg.in/dealancer/validate.v2" -) - -// Validator is a service -type Validator struct { - Config *dagconfigservice.DagConfigService -} - -// CheckDuplicates checks for duplicate Relationship and Service IDs in a constellation file. -func (dag *Validator) CheckDuplicates() (duplicates []string) { - IDs := []string{string(dag.Config.ID)} - - for _, i := range dag.Config.Services { - _, exists := Find(IDs, i.ID) - if exists { - duplicates = append(duplicates, i.ID) - } else { - IDs = append(IDs, i.ID) - } - } - - for _, i := range dag.Config.Relationships { - _, exists := Find(IDs, i.ID) - if exists { - duplicates = append(duplicates, i.ID) - } else { - IDs = append(IDs, i.ID) - } - } - - return -} - -// CheckServiceExists loops through each Relationship and checks if the services are declared. -func (dag *Validator) CheckServiceExists() (missing map[string][]string) { - missing = make(map[string][]string) - IDs := []string{} - - for _, i := range dag.Config.Services { - IDs = append(IDs, i.ID) - } - - for _, i := range dag.Config.Relationships { - _, exists := Find(IDs, i.To) - if !exists { - missing[i.ID] = append(missing[i.ID], i.To) - } - - _, exists = Find(IDs, i.From) - if !exists { - missing[i.ID] = append(missing[i.ID], i.From) - } - } - - return -} - -// ValidateModel checks if constellation has all required felids -func (dag *Validator) ValidateModel() error { - return validate.Validate(dag.Config) -} - -// Find takes a slice and looks for an element in it. If found it will -// return it's key, otherwise it will return -1 and a bool of false. -func Find(slice []string, val string) (int, bool) { - for i, item := range slice { - if item == val { - return i, true - } - } - return -1, false -} diff --git a/internal/validationservice/validationservice_test.go b/internal/validationservice/validationservice_test.go deleted file mode 100644 index d8b9dec..0000000 --- a/internal/validationservice/validationservice_test.go +++ /dev/null @@ -1,155 +0,0 @@ -package validationservice - -import ( - config "github.com/microsoft/abstrakt/internal/dagconfigservice" - "testing" -) - -func TestForDuplicatIDsInServices(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/valid.yaml") - - service := Validator{Config: testData} - duplicates := service.CheckDuplicates() - - if duplicates != nil { - t.Error("No duplicates should be found.") - } -} - -func TestServicesExists(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/valid.yaml") - - service := Validator{Config: testData} - missing := service.CheckServiceExists() - - if len(missing) != 0 { - t.Error("No missing services should be found.") - } -} - -func TestSchemaChecks(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/valid.yaml") - - service := Validator{Config: testData} - err := service.ValidateModel() - - if err != nil { - t.Errorf("Model validation should not return errors: %v", err.Error()) - } -} - -func TestForDuplicatIDsInServicesFail(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/duplicate/servIds.yaml") - - service := Validator{Config: testData} - duplicates := service.CheckDuplicates() - - if duplicates == nil { - t.Errorf("There should be %v duplicate IDs found", len(testData.Services)) - } -} - -func TestForDuplicatIDsInRelationshipsFail(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/duplicate/relIds.yaml") - - service := Validator{Config: testData} - duplicates := service.CheckDuplicates() - - if duplicates == nil { - t.Errorf("There should be %v duplicate IDs found", len(testData.Relationships)) - } -} - -func TestForDuplicatIDsFail(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/duplicate/servRelIds.yaml") - - service := Validator{Config: testData} - duplicates := service.CheckDuplicates() - - if duplicates == nil { - t.Error("There should be 2 duplicate IDs found") - } -} - -func TestServicesExistsFail(t *testing.T) { - expected := "Azure Event Hub" - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/missing/relServRefId.yaml") - - service := Validator{Config: testData} - missing := service.CheckServiceExists() - - foundID := missing["Event Hubs to Event Logger Link"][0] - - if len(missing) != 1 { - t.Error("There should be only 1 missing services found") - } else if foundID != expected { - t.Errorf("Incorrect reference found. Expected: %v \nGot: %v", expected, foundID) - } -} - -func TestSchemaMissingDagName(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/missingName.yaml") - - service := Validator{Config: testData} - err := service.ValidateModel() - - if err == nil { - t.Error("Model validation should be invalid") - } -} - -func TestSchemaMissingDagID(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/missing/id.yaml") - - service := Validator{Config: testData} - err := service.ValidateModel() - - if err == nil { - t.Error("Model validation should be invalid") - } -} - -func TestSchemaMissingService(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/missing/serv.yaml") - - service := Validator{Config: testData} - err := service.ValidateModel() - - if err == nil { - t.Error("Model validation should be invalid") - } -} - -func TestSchemaMissingServiceID(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/missing/servId.yaml") - - service := Validator{Config: testData} - err := service.ValidateModel() - - if err == nil { - t.Error("Model validation should be invalid") - } -} - -func TestSchemaMissingRelationshipID(t *testing.T) { - testData := &config.DagConfigService{} - _ = testData.LoadDagConfigFromFile("testdata/missing/relId.yaml") - - service := Validator{Config: testData} - err := service.ValidateModel() - - if err == nil { - t.Error("Model validation should be invalid") - } -} From ae85792efe684cf8ecfea9acbfc7bcc03e943064 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 14 Jan 2020 11:31:13 +1100 Subject: [PATCH 05/85] Updated tests to use asserts. --- cmd/compose_test.go | 34 +++-------- cmd/diff_test.go | 58 ++++--------------- cmd/validate_test.go | 5 +- cmd/version_test.go | 6 +- cmd/visualise_test.go | 25 ++------ go.mod | 1 + internal/compose/compose.go | 2 +- internal/compose/compose_test.go | 27 +++------ .../constellation/constellation_test.go | 5 +- internal/platform/constellation/find_test.go | 14 ++--- internal/platform/constellation/graph.go | 2 +- internal/platform/constellation/graph_test.go | 14 ++--- .../platform/constellation/validate_test.go | 55 ++++++------------ internal/platform/mapper/mapper_test.go | 5 +- 14 files changed, 73 insertions(+), 180 deletions(-) diff --git a/cmd/compose_test.go b/cmd/compose_test.go index dc9aba4..6e817a8 100644 --- a/cmd/compose_test.go +++ b/cmd/compose_test.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/stretchr/testify/assert" "testing" ) @@ -12,10 +13,7 @@ func TestComposeCommandReturnsErrorIfTemplateTypeIsInvalid(t *testing.T) { defer helpers.CleanTempTestFiles(t, tdir) output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-if-template-type-is-invalid", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) - - if err == nil { - t.Errorf("Did not received expected error. \nGot:\n %v", output) - } + assert.Errorf(t, err, "Did not received expected error. \nGot:\n %v", output) } func TestComposeCommandDoesNotErrorIfTemplateTypeIsEmptyOrHelm(t *testing.T) { @@ -25,33 +23,24 @@ func TestComposeCommandDoesNotErrorIfTemplateTypeIsEmptyOrHelm(t *testing.T) { defer helpers.CleanTempTestFiles(t, tdir) output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + assert.NoErrorf(t, err, "Did not expect error:\n %v\n output: %v", err, output) - if err != nil { - t.Errorf("Did not expect error:\n %v\n output: %v", err, output) - } templateType = "helm" - output, err = helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) - if err != nil { - t.Errorf("Did not expect error:\n %v\n output: %v", err, output) - } + output, err = helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + assert.NoErrorf(t, err, "Did not expect error:\n %v\n output: %v", err, output) } func TestComposeCommandReturnsErrorWithInvalidFilePaths(t *testing.T) { output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-with-invalid-files", "-f", "invalid", "-m", "invalid", "-o", "invalid") - - if err == nil { - t.Errorf("Did not received expected error. \nGot:\n %v", output) - } + assert.Errorf(t, err, "Did not received expected error. \nGot:\n %v", output) } -// TODO bug #43: figure out how to make this test work reliably. -// Something weird is making this test fail when run along with other tests in the package. -// It passes whenever it runs on it's own. func TestComposeCmdVerifyRequiredFlags(t *testing.T) { expected := "required flag(s) \"constellationFilePath\", \"mapsFilePath\", \"outputPath\" not set" output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "") + if err != nil { helpers.CheckStringContains(t, err.Error(), expected) } else { @@ -65,9 +54,7 @@ func TestComposeCmdWithValidFlags(t *testing.T) { defer helpers.CleanTempTestFiles(t, tdir) output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-flags", "-f", constellationPath, "-m", mapsPath, "-o", tdir) - if err != nil { - t.Errorf("error: \n %v\noutput:\n %v\n", err, output) - } + assert.NoErrorf(t, err, "error: \n %v\noutput:\n %v\n", err, output) } func TestComposeWithRealFiles(t *testing.T) { @@ -76,8 +63,5 @@ func TestComposeWithRealFiles(t *testing.T) { defer helpers.CleanTempTestFiles(t, tdir) output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-real-files", "-f", constellationPath, "-m", mapsPath, "-o", tdir) - if err != nil { - t.Errorf("error: \n %v\noutput:\n %v\n", err, output) - } - + assert.NoErrorf(t, err, "error: \n %v\noutput:\n %v\n", err, output) } diff --git a/cmd/diff_test.go b/cmd/diff_test.go index 67ebb93..a4ca503 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -1,11 +1,11 @@ package cmd import ( - "fmt" set "github.com/deckarep/golang-set" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/sirupsen/logrus/hooks/test" + "github.com/stretchr/testify/assert" "io/ioutil" "os" "path" @@ -20,15 +20,10 @@ func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { hook := test.NewGlobal() _, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", constellationPathOrg, "-n", constellationPathNew) - // fmt.Println(hook.LastEntry().Message) - // fmt.Println(testDiffComparisonOutputString) - if err != nil { t.Error("Did not receive output") } else { - if !helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, hook.LastEntry().Message) { - t.Errorf("Expcted output and produced output do not match : expected %s produced %s", testDiffComparisonOutputString, hook.LastEntry().Message) - } + assert.Truef(t, helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, hook.LastEntry().Message), "Expcted output and produced output do not match : expected %s produced %s", testDiffComparisonOutputString, hook.LastEntry().Message) // Did use this initially but wont work with the strongs output from the graphviz library as the sequence of entries in the output can change // while the sequence may change the result is still valid and the same so am usinga local comparison function to get around this problem // checkStringContains(t, hook.LastEntry().Message, testDiffComparisonOutputString) @@ -54,8 +49,6 @@ func localPrepareRealFilesForTest(t *testing.T) (string, string, string, string) t.Fatal(err2) } - fmt.Print(cwd) - constellationPathOrg := path.Join(cwd, "../sample/constellation/sample_constellation.yaml") constellationPathNew := path.Join(cwd, "../sample/constellation/sample_constellation_changed.yaml") mapsPath := path.Join(cwd, "../sample/constellation/sample_constellation_maps.yaml") @@ -116,15 +109,11 @@ func TestGetComparisonSets(t *testing.T) { dsGraphOrg := new(constellation.Config) err := dsGraphOrg.LoadString(testOrgDagStr) - if err != nil { - t.Errorf("dagConfigService failed to load dag from test string %s", err) - } + assert.NoErrorf(t, err, "dagConfigService failed to load dag from test string %s", err) dsGraphNew := new(constellation.Config) err = dsGraphNew.LoadString(testNewDagStr) - if err != nil { - t.Errorf("dagConfigService failed to load file %s", err) - } + assert.NoErrorf(t, err, "dagConfigService failed to load file %s", err) //construct sets struct for loaded constellation loadedSets := &setsForComparison{} @@ -136,30 +125,12 @@ func TestGetComparisonSets(t *testing.T) { // function being tested fillComparisonSets(dsGraphOrg, dsGraphNew, loadedSets) - if !knownSets.setCommonSvcs.Equal(loadedSets.setCommonSvcs) { - t.Errorf("Common services - did not match between expected result and input yaml") - } - - if !knownSets.setCommonRels.Equal(loadedSets.setCommonRels) { - t.Errorf("Common relationships - did not match between expected result and input yaml") - } - - if !knownSets.setAddedSvcs.Equal(loadedSets.setAddedSvcs) { - t.Errorf("Added services - did not match between expected result and input yaml") - } - - if !knownSets.setAddedRels.Equal(loadedSets.setAddedRels) { - t.Errorf("Added relationships - did not match between expected result and input yaml") - } - - if !knownSets.setDelSvcs.Equal(loadedSets.setDelSvcs) { - t.Errorf("Deleted services - did not match between expected result and input yaml") - } - - if !knownSets.setDelRels.Equal(loadedSets.setDelRels) { - t.Errorf("Deleted relationships - did not match between expected result and input yaml") - } - + assert.True(t, knownSets.setCommonSvcs.Equal(loadedSets.setCommonSvcs), "Common services - did not match between expected result and input yaml") + assert.True(t, knownSets.setCommonRels.Equal(loadedSets.setCommonRels), "Common relationships - did not match between expected result and input yaml") + assert.True(t, knownSets.setAddedSvcs.Equal(loadedSets.setAddedSvcs), "Added services - did not match between expected result and input yaml") + assert.True(t, knownSets.setAddedRels.Equal(loadedSets.setAddedRels), "Added relationships - did not match between expected result and input yaml") + assert.True(t, knownSets.setDelSvcs.Equal(loadedSets.setDelSvcs), "Deleted services - did not match between expected result and input yaml") + assert.True(t, knownSets.setDelRels.Equal(loadedSets.setDelRels), "Deleted relationships - did not match between expected result and input yaml") } // testGraphWithChanges - test diff comparison function @@ -173,9 +144,7 @@ func TestGraphWithChanges(t *testing.T) { dsGraphNew := new(constellation.Config) err = dsGraphNew.LoadString(testNewDagStr) - if err != nil { - t.Errorf("dagConfigService failed to load file %s", err) - } + assert.NoErrorf(t, err, "dagConfigService failed to load file %s", err) //construct sets struct for loaded constellation loadedSets := &setsForComparison{} @@ -185,9 +154,7 @@ func TestGraphWithChanges(t *testing.T) { resString := createGraphWithChanges(dsGraphNew, loadedSets) - if !helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString) { - t.Errorf("Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) - } + assert.Truef(t, helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString), "Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) } // Utility to populate comparison sets with expected/known result @@ -215,7 +182,6 @@ func populateComparisonSets(target *setsForComparison) { target.setDelRels = set.NewSet() target.setDelRels.Add("9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" + "|" + "3aa1e546-1ed5-4d67-a59c-be0d5905b490") target.setDelRels.Add("3aa1e546-1ed5-4d67-a59c-be0d5905b490" + "|" + "a268fae5-2a82-4a3e-ada7-a52eeb7019ac") - } // Sample DAG file data - original file diff --git a/cmd/validate_test.go b/cmd/validate_test.go index 13960d8..d1677b8 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/stretchr/testify/assert" "testing" ) @@ -14,9 +15,7 @@ func TestValidateCommand(t *testing.T) { output, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) - if err != nil { - t.Errorf("Did not received expected error. \nGot:\n %v", output) - } + assert.NoErrorf(t, err, "Did not received expected error. \nGot:\n %v", output) _, err = helpers.ExecuteCommand(newValidateCmd().cmd, "-f", "does-not-exist") diff --git a/cmd/version_test.go b/cmd/version_test.go index 156323b..bbf18df 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/sirupsen/logrus/hooks/test" + "github.com/stretchr/testify/assert" "os" "testing" ) @@ -16,10 +17,7 @@ func TestMain(m *testing.M) { func TestVersion(t *testing.T) { expected := "0.0.1" version := Version() - - if version != expected { - t.Errorf("Did not find correct abstrakt version. Expected %v, got %v", expected, version) - } + assert.Equal(t, expected, version) } func TestVersionCmd(t *testing.T) { diff --git a/cmd/visualise_test.go b/cmd/visualise_test.go index df0ddd2..95d8ed4 100644 --- a/cmd/visualise_test.go +++ b/cmd/visualise_test.go @@ -1,10 +1,10 @@ package cmd import ( - "fmt" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/sirupsen/logrus/hooks/test" + "github.com/stretchr/testify/assert" "io/ioutil" "os" "path/filepath" @@ -22,9 +22,7 @@ func TestVisualiseCmdWithAllRequirementsNoError(t *testing.T) { if err != nil { t.Error("Did not receive output") } else { - if !helpers.CompareGraphOutputAsSets(validGraphString, hook.LastEntry().Message) { - t.Errorf("Expcted output and produced output do not match : expected %s produced %s", validGraphString, hook.LastEntry().Message) - } + assert.Truef(t, helpers.CompareGraphOutputAsSets(validGraphString, hook.LastEntry().Message), "Expcted output and produced output do not match : expected %s produced %s", validGraphString, hook.LastEntry().Message) } } @@ -65,22 +63,15 @@ func TestFileExists(t *testing.T) { //Create a file to test against err := ioutil.WriteFile(testValidFilename, testData, 0644) - if err != nil { - fmt.Println("Could not create output testing file, cannot proceed") - t.Error(err) - } + assert.NoError(t, err, "Could not create output testing file, cannot proceed") //Test that a valid file (created above) can be seen var result bool = helpers.FileExists(testValidFilename) //Expecting true - file does exists - if result == false { - t.Errorf("Test file does exist but testFile returns that it does not") - } + assert.True(t, result, "Test file does exist but testFile returns that it does not") //Test that an invalid file (does not exist) is not seen result = helpers.FileExists(testInvalidFilename) //Expecting false - file does not exist - if result != false { - t.Errorf("Test file does not exist but testFile says it does") - } + assert.False(t, result, "Test file does not exist but testFile says it does") err = os.Remove(testValidFilename) if err != nil { @@ -88,10 +79,7 @@ func TestFileExists(t *testing.T) { } result = helpers.FileExists(testValidFilename) //Expecting false - file has been removed - if result == true { - t.Errorf("Test file has been removed but fileExists is finding it") - } - + assert.False(t, result, "Test file has been removed but fileExists is finding it") } func TestParseYaml(t *testing.T) { @@ -108,7 +96,6 @@ func TestParseYaml(t *testing.T) { len(retConfig.Relationships) != 1 { t.Errorf("YAML did not parse correctly and it should have") } - } // Sample DAG file data diff --git a/go.mod b/go.mod index 539a06b..8839c19 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/spf13/cobra v0.0.5 github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/viper v1.4.0 + github.com/stretchr/testify v1.4.0 golang.org/x/crypto v0.0.0-20191028145041-f83a4685e152 golang.org/x/sys v0.0.0-20191112214154-59a1497f0cea // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 5f0b20f..0cacb81 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -3,7 +3,7 @@ package compose import ( "errors" "fmt" - "github.com/microsoft/abstrakt/internal/chart" + "github.com/microsoft/abstrakt/internal/platform/chart" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/platform/mapper" helm "helm.sh/helm/v3/pkg/chart" diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go index a27bf77..f99c01f 100644 --- a/internal/compose/compose_test.go +++ b/internal/compose/compose_test.go @@ -3,6 +3,7 @@ package compose_test import ( "fmt" "github.com/microsoft/abstrakt/internal/compose" + "github.com/stretchr/testify/assert" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/chartutil" @@ -28,17 +29,13 @@ func TestComposeService(t *testing.T) { comp := new(compose.Composer) _, err = comp.Build("test", tdir) - if err == nil { - t.Errorf("Compose should fail if not yet loaded") - } + assert.NoError(t, err, "Compose should fail if not yet loaded") _ = comp.LoadString(test01DagStr, configMapTest01String) h, err := comp.Build("test", tdir) - if err != nil { - t.Errorf("Compose should have loaded") - } + assert.NoError(t, err, "Compose should have loaded") _ = chartutil.SaveDir(h, tdir) h, _ = loader.LoadDir(tdir) @@ -74,9 +71,7 @@ func TestHelmLibCompose(t *testing.T) { t.Fatalf("Failed to load newly created chart %q: %s", c, err) } - if mychart.Name() != "foo" { - t.Errorf("Expected name to be 'foo', got %q", mychart.Name()) - } + assert.Equalf(t, "foo", mychart.Name(), "Expected name to be 'foo', got %q", mychart.Name()) for _, f := range []string{ chartutil.ChartfileName, @@ -113,21 +108,15 @@ func TestHelmLibCompose(t *testing.T) { func TestLoadFromString(t *testing.T) { comp := new(compose.Composer) - err := comp.LoadString(test01DagStr, configMapTest01String) - if err != nil { - t.Errorf("Error: %v", err) - } + err := comp.LoadString(test01DagStr, configMapTest01String) + assert.NoErrorf(t, err, "Error: %v", err) err = comp.LoadString("sfdsd", configMapTest01String) - if err == nil { - t.Errorf("Didn't get error when should") - } + assert.Error(t, err, "Didn't get error when should") err = comp.LoadString(test01DagStr, "sdfsdf") - if err == nil { - t.Errorf("Didn't get error when should") - } + assert.Error(t, err, "Didn't get error when should") } const test01DagStr = `Name: "Azure Event Hubs Sample" diff --git a/internal/platform/constellation/constellation_test.go b/internal/platform/constellation/constellation_test.go index bb26031..2551424 100644 --- a/internal/platform/constellation/constellation_test.go +++ b/internal/platform/constellation/constellation_test.go @@ -3,6 +3,7 @@ package constellation_test import ( "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/guid" + "github.com/stretchr/testify/assert" "io/ioutil" "reflect" "testing" @@ -37,9 +38,7 @@ func TestNewDagConfigFromString(t *testing.T) { t.Errorf("LoadDagConfigFromString() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(dag, tt.wantRet) { - t.Errorf("LoadDagConfigFromString() =\n%#v,\nWant:\n%#v\n", dag, tt.wantRet) - } + assert.Truef(t, reflect.DeepEqual(dag, tt.wantRet), "LoadDagConfigFromString() =\n%#v,\nWant:\n%#v\n", dag, tt.wantRet) }) } } diff --git a/internal/platform/constellation/find_test.go b/internal/platform/constellation/find_test.go index f3289fa..8bda99b 100644 --- a/internal/platform/constellation/find_test.go +++ b/internal/platform/constellation/find_test.go @@ -2,6 +2,7 @@ package constellation_test import ( "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/stretchr/testify/assert" "testing" ) @@ -11,9 +12,7 @@ func TestRelationshipFinding(t *testing.T) { rel1 := dag.FindRelationshipByFromName("Event Generator") rel2 := dag.FindRelationshipByToName("Azure Event Hub") - if rel1[0].From != rel2[0].From || rel1[0].To != rel2[0].To { - t.Error("Relationships were not correctly resolved") - } + assert.Condition(t, func() bool { return !(rel1[0].From != rel2[0].From || rel1[0].To != rel2[0].To) }, "Relationships were not correctly resolved") } func TestMultipleInstanceInRelationships(t *testing.T) { @@ -32,11 +31,6 @@ func TestMultipleInstanceInRelationships(t *testing.T) { from := dag.FindRelationshipByFromName("Event Generator") to := dag.FindRelationshipByToName("Event Logger") - if len(from) != 2 { - t.Error("Event Generator did not have the correct number of `From` relationships") - } - - if len(to) != 2 { - t.Error("Event Logger did not have the correct number of `To` relationships") - } + assert.EqualValues(t, 2, len(from), "Event Generator did not have the correct number of `From` relationships") + assert.EqualValues(t, 2, len(to), "Event Logger did not have the correct number of `To` relationships") } diff --git a/internal/platform/constellation/graph.go b/internal/platform/constellation/graph.go index 5bad085..46fe32a 100644 --- a/internal/platform/constellation/graph.go +++ b/internal/platform/constellation/graph.go @@ -3,8 +3,8 @@ package constellation import ( "fmt" "github.com/awalterschulze/gographviz" - "strings" "io" + "strings" ) // GenerateGraph - function to take a dagconfigService structure and create a graph object that contains the diff --git a/internal/platform/constellation/graph_test.go b/internal/platform/constellation/graph_test.go index 8c38ca8..126e23b 100644 --- a/internal/platform/constellation/graph_test.go +++ b/internal/platform/constellation/graph_test.go @@ -4,6 +4,7 @@ import ( "bytes" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/stretchr/testify/assert" "testing" ) @@ -11,6 +12,9 @@ func TestGenerateGraph(t *testing.T) { retConfig := new(constellation.Config) err := retConfig.LoadFile("testdata/valid.yaml") + + assert.NoError(t, err) + if err != nil { panic(err) } @@ -20,14 +24,8 @@ func TestGenerateGraph(t *testing.T) { cmpString := test02ConstGraphString retString, err := retConfig.GenerateGraph(out) - if err != nil { - t.Errorf("Should not recieve error: %v", err) - } - - if !helpers.CompareGraphOutputAsSets(cmpString, retString) { - t.Errorf("Input graph did not generate expected output graphviz representation") - t.Errorf("Expected:\n%v \nGot:\n%v", cmpString, retString) - } + assert.NoErrorf(t, err, "Should not recieve error: %v", err) + assert.True(t, helpers.CompareGraphOutputAsSets(cmpString, retString), "Input graph did not generate expected output graphviz representation\nExpected:\n%v \nGot:\n%v", cmpString, retString) } const test02ConstGraphString = `digraph Azure_Event_Hubs_Sample { diff --git a/internal/platform/constellation/validate_test.go b/internal/platform/constellation/validate_test.go index 77cb27a..7a26b68 100644 --- a/internal/platform/constellation/validate_test.go +++ b/internal/platform/constellation/validate_test.go @@ -2,6 +2,7 @@ package constellation_test import ( "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/stretchr/testify/assert" "testing" ) @@ -11,9 +12,7 @@ func TestForDuplicatIDsInServices(t *testing.T) { duplicates := testData.CheckDuplicates() - if duplicates != nil { - t.Error("No duplicates should be found.") - } + assert.Nil(t, duplicates, "No duplicates should be found.") } func TestServicesExists(t *testing.T) { @@ -22,9 +21,7 @@ func TestServicesExists(t *testing.T) { missing := testData.CheckServiceExists() - if len(missing) != 0 { - t.Error("No missing services should be found.") - } + assert.Empty(t, missing, "No missing services should be found.") } func TestSchemaChecks(t *testing.T) { @@ -33,9 +30,7 @@ func TestSchemaChecks(t *testing.T) { err := testData.ValidateModel() - if err != nil { - t.Errorf("Model validation should not return errors: %v", err.Error()) - } + assert.NoError(t, err, "Model validation should not return errors") } func TestForDuplicatIDsInServicesFail(t *testing.T) { @@ -44,9 +39,8 @@ func TestForDuplicatIDsInServicesFail(t *testing.T) { duplicates := testData.CheckDuplicates() - if duplicates == nil { - t.Errorf("There should be %v duplicate IDs found", len(testData.Services)) - } + assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 2) + assert.Equal(t, 2, len(duplicates)) } func TestForDuplicatIDsInRelationshipsFail(t *testing.T) { @@ -55,9 +49,8 @@ func TestForDuplicatIDsInRelationshipsFail(t *testing.T) { duplicates := testData.CheckDuplicates() - if duplicates == nil { - t.Errorf("There should be %v duplicate IDs found", len(testData.Relationships)) - } + assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 1) + assert.Equal(t, 1, len(duplicates)) } func TestForDuplicatIDsFail(t *testing.T) { @@ -66,9 +59,8 @@ func TestForDuplicatIDsFail(t *testing.T) { duplicates := testData.CheckDuplicates() - if duplicates == nil { - t.Error("There should be 2 duplicate IDs found") - } + assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 1) + assert.Equal(t, 1, len(duplicates)) } func TestServicesExistsFail(t *testing.T) { @@ -80,11 +72,8 @@ func TestServicesExistsFail(t *testing.T) { foundID := missing["Event Hubs to Event Logger Link"][0] - if len(missing) != 1 { - t.Error("There should be only 1 missing services found") - } else if foundID != expected { - t.Errorf("Incorrect reference found. Expected: %v \nGot: %v", expected, foundID) - } + assert.Equal(t, 1, len(missing), "There should be only 1 missing services found") + assert.Equalf(t, expected, foundID, "Incorrect reference found\nExpected: %v \nGot: %v", expected, foundID) } func TestSchemaMissingDagName(t *testing.T) { @@ -93,9 +82,7 @@ func TestSchemaMissingDagName(t *testing.T) { err := testData.ValidateModel() - if err == nil { - t.Error("Model validation should be invalid") - } + assert.Error(t, err, "Model validation should be invalid") } func TestSchemaMissingDagID(t *testing.T) { @@ -104,9 +91,7 @@ func TestSchemaMissingDagID(t *testing.T) { err := testData.ValidateModel() - if err == nil { - t.Error("Model validation should be invalid") - } + assert.Error(t, err, "Model validation should be invalid") } func TestSchemaMissingService(t *testing.T) { @@ -115,9 +100,7 @@ func TestSchemaMissingService(t *testing.T) { err := testData.ValidateModel() - if err == nil { - t.Error("Model validation should be invalid") - } + assert.Error(t, err, "Model validation should be invalid") } func TestSchemaMissingServiceID(t *testing.T) { @@ -126,9 +109,7 @@ func TestSchemaMissingServiceID(t *testing.T) { err := testData.ValidateModel() - if err == nil { - t.Error("Model validation should be invalid") - } + assert.Error(t, err, "Model validation should be invalid") } func TestSchemaMissingRelationshipID(t *testing.T) { @@ -137,7 +118,5 @@ func TestSchemaMissingRelationshipID(t *testing.T) { err := testData.ValidateModel() - if err == nil { - t.Error("Model validation should be invalid") - } + assert.Error(t, err, "Model validation should be invalid") } diff --git a/internal/platform/mapper/mapper_test.go b/internal/platform/mapper/mapper_test.go index 719bb03..6c17bf4 100644 --- a/internal/platform/mapper/mapper_test.go +++ b/internal/platform/mapper/mapper_test.go @@ -3,6 +3,7 @@ package mapper_test import ( "github.com/microsoft/abstrakt/internal/platform/mapper" "github.com/microsoft/abstrakt/internal/tools/guid" + "github.com/stretchr/testify/assert" "reflect" "testing" ) @@ -32,9 +33,7 @@ func TestMapFromString(t *testing.T) { t.Errorf("LoadMapFromString() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(mapper, tt.wantRet) { - t.Errorf("LoadMapFromString() = %v, want %v", mapper, tt.wantRet) - } + assert.Truef(t, reflect.DeepEqual(mapper, tt.wantRet), "LoadMapFromString() = %v, want %v", mapper, tt.wantRet) }) } } From e4c9b0c030c8436ab38994f42ce116268c558799 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 15 Jan 2020 11:13:42 +1100 Subject: [PATCH 06/85] Shifted diff's core logic to constellation package. --- cmd/diff.go | 204 +---------------- cmd/diff_test.go | 207 +----------------- internal/platform/constellation/diff.go | 192 ++++++++++++++++ internal/platform/constellation/diff_test.go | 109 +++++++++ .../constellation/testdata/diff/modified.yaml | 48 ++++ .../constellation/testdata/diff/original.yaml | 38 ++++ internal/tools/helpers/test.go | 26 +++ 7 files changed, 420 insertions(+), 404 deletions(-) create mode 100644 internal/platform/constellation/diff.go create mode 100644 internal/platform/constellation/diff_test.go create mode 100644 internal/platform/constellation/testdata/diff/modified.yaml create mode 100644 internal/platform/constellation/testdata/diff/original.yaml diff --git a/cmd/diff.go b/cmd/diff.go index 81036fc..7b37ddf 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -8,8 +8,6 @@ package cmd import ( "bytes" "fmt" - "github.com/awalterschulze/gographviz" - set "github.com/deckarep/golang-set" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/microsoft/abstrakt/internal/tools/logger" @@ -25,15 +23,6 @@ type diffCmd struct { *baseCmd } -type setsForComparison struct { - setCommonSvcs set.Set - setCommonRels set.Set - setAddedSvcs set.Set - setAddedRels set.Set - setDelSvcs set.Set - setDelRels set.Set -} - func newDiffCmd() *diffCmd { cc := &diffCmd{} @@ -90,7 +79,12 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP logger.Output(resStringNew) } - resStringDiff := compareConstellations(dsGraphOrg, dsGraphNew) + resStringDiff, err := constellation.CompareConstellations(dsGraphOrg, dsGraphNew) + + if err != nil { + return err + } + logger.Output(resStringDiff) return nil @@ -106,189 +100,3 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP return cc } - -//compareConstellations -func compareConstellations(dsOrg *constellation.Config, dsNew *constellation.Config) string { - sets := &setsForComparison{} - - // populate comparison sets with changes between original and new graph - fillComparisonSets(dsOrg, dsNew, sets) - - // build the graphviz output from new graph and comparison sets - resString := createGraphWithChanges(dsNew, sets) - - return resString -} - -// fillComparisonSets - loads provided set struct with data from the constellations and then determines the various differences between the -// sets (original constellation and new) to help detemine what has been added, removed or changed. -func fillComparisonSets(dsOrg *constellation.Config, dsNew *constellation.Config, sets *setsForComparison) { - setOrgSvcs, setOrgRel := createSet(dsOrg) - setNewSvcs, setNewRel := createSet(dsNew) - - // "Added" means in the new constellation but not the original one - // "Deleted" means something is present in the original constellation but not in the new constellation - // Services - sets.setCommonSvcs = setOrgSvcs.Intersect(setNewSvcs) //present in both - sets.setAddedSvcs = setNewSvcs.Difference(setOrgSvcs) //pressent in new but not in original - sets.setDelSvcs = setOrgSvcs.Difference(setNewSvcs) //present in original but not in new - // Relationships - sets.setCommonRels = setOrgRel.Intersect(setNewRel) //present in both - sets.setAddedRels = setNewRel.Difference(setOrgRel) //pressent in new but not in original - sets.setDelRels = setOrgRel.Difference(setNewRel) //present in original but not in new -} - -// createSet - utility function used to create a pair of result sets (services + relationships) based on an input constellation DAG -func createSet(dsGraph *constellation.Config) (set.Set, set.Set) { - - // Create sets to hold services and relationships - used to find differences between old and new using intersection and difference operations - retSetServices := set.NewSet() - retSetRelationships := set.NewSet() - - //Store all services in the services set - for _, v := range dsGraph.Services { - retSetServices.Add(v.ID) - } - - //Store relationships in the relationship set - for _, v := range dsGraph.Relationships { - retSetRelationships.Add(v.From + "|" + v.To) - } - - return retSetServices, retSetRelationships -} - -// createGraphWithChanges - use both input constellations (new and original) as well as the comparison sets to create -// a dag that can be visualised. It uses the comparison sets to identify additions, deletions and changes between the original -// and new constellations. -func createGraphWithChanges(newGraph *constellation.Config, sets *setsForComparison) string { - // Lookup is used to map IDs to names. Names are easier to visualise but IDs are more important to ensure the - // presented constellation is correct and IDs are used to link nodes together - lookup := make(map[string]string) - g := gographviz.NewGraph() - - // Replace spaces with underscores, names with spaces can break graphviz engines - if err := g.SetName(strings.Replace(newGraph.Name, " ", "_", -1) + "_diff"); err != nil { - logger.Fatalf("error setting graph name: %v", err) - } - // Attribute in graphviz to change graph orientation - LR indicates Left to Right. Default is top to bottom - if err := g.AddAttr(g.Name, "rankdir", "LR"); err != nil { - logger.Fatalf("error adding node: %v", err) - } - - // Make the graph directed (a constellation is DAG) - if err := g.SetDir(true); err != nil { - logger.Fatalf("error: %v", err) - } - - // Add all services from the new constellation - // - New services - highlight with colour (i.e in setAddedSvcs) - // - Deleted services (i.e. in setDelSvcs) - include and format appropriately - for _, v := range newGraph.Services { - logger.Debugf("Adding node %s", v.ID) - newName := strings.Replace(v.ID, " ", "_", -1) // Replace spaces in names with underscores, names with spaces can break graphviz engines) - - // attributes are graphviz specific that control formatting. The linrary used requires then in the form of a map passed as an argument so it needs - // to be built before adding the item - attrs := make(map[string]string) - - // Check if the service is new to this constellation - if sets.setAddedSvcs.Contains(newName) { - attrs["color"] = "\"#d8ffa8\"" - } - attrs["label"] = "\"" + v.Type + "\n" + v.ID + "\"" - fillShapeAndStyleForNodeType(v.Type, attrs) - - lookup[v.ID] = newName - err := g.AddNode(newGraph.Name, "\""+newName+"\"", attrs) //Surround names/labels with quotes, stops graphviz seeing special characters and breaking - if err != nil { - logger.Fatalf("error: %v", err) - } - - } - - //======================================== process deleted services ========================================== - //Process services that have been removed from the original constellation - for v := range sets.setDelSvcs.Iter() { - logger.Debug(v) - vString, _ := v.(string) - newName := strings.Replace(vString, " ", "_", -1) - attrs := make(map[string]string) - attrs["color"] = "\"#ff9494\"" - fillShapeAndStyleForNodeType("", attrs) - logger.Debug("Adding deleted service ", newName) - if err := g.AddNode(newGraph.Name, "\""+newName+"\"", attrs); err != nil { - logger.Fatalf("error adding node: %v", err) - } - - } - - //======================================== process relationships ========================================== - // Add Relationships from the new constellation - // - New relationships - highlight with colour (i.e. in setAddedSvcs) - // - Deleted relationships (i.e. in setDelRels) - include and format appropriately - for _, v := range newGraph.Relationships { - logger.Debugf("Adding relationship from %s ---> %s", v.From, v.To) - //Surround names/labels with quotes, stops graphviz seeing special characters and breaking - localFrom := "\"" + lookup[v.From] + "\"" - localTo := "\"" + lookup[v.To] + "\"" - - attrs := make(map[string]string) - // Relationship is stored in the map as source|destination - both are needed to tell if a relationship is new (this is how they are stored in the sets created earlier) - relLookupName := lookup[v.From] + "|" + lookup[v.To] - - // Check if the relationship is new (it will then not be in the common list of relationships between old and new constellation) - if sets.setAddedRels.Contains(relLookupName) { - attrs["color"] = "\"#d8ffa8\"" - } - - err := g.AddEdge(localFrom, localTo, true, attrs) - if err != nil { - logger.Fatalf("error: %v", err) - } - } - - //======================================== process deleted relationships ===================================== - // Deleted relationships - for v := range sets.setDelRels.Iter() { - logger.Debug(v) - vString := strings.Replace(v.(string), " ", "_", -1) - if len(strings.Split(vString, "|")) != 2 { - logger.Fatal("Relationships string should be two items separated by | but got ", vString) - } - newFrom := "\"" + strings.Split(vString, "|")[0] + "\"" - newTo := "\"" + strings.Split(vString, "|")[1] + "\"" - - attrs := make(map[string]string) - attrs["color"] = "\"#ff9494\"" - logger.Debug("Adding deleted service relationship from ", newFrom, " to ", newTo) - err := g.AddEdge(newFrom, newTo, true, attrs) - if err != nil { - logger.Fatalf("error: %v", err) - } - } - - // Produce resulting graph in dot notation format - return g.String() -} - -// Populate attrs map with additional attributes based on the node type -// Easier to change in a single place -func fillShapeAndStyleForNodeType(nodeType string, existAttrs map[string]string) { - - switch nodeType { - case "EventLogger": - existAttrs["shape"] = "rectangle" - existAttrs["style"] = "\"rounded, filled\"" - case "EventGenerator": - existAttrs["shape"] = "rectangle" - existAttrs["style"] = "\"rounded, filled\"" - case "EventHub": - existAttrs["shape"] = "rectangle" - existAttrs["style"] = "\"rounded, filled\"" - default: - existAttrs["shape"] = "rectangle" - existAttrs["style"] = "\"rounded, filled\"" - } - -} diff --git a/cmd/diff_test.go b/cmd/diff_test.go index a4ca503..74bb849 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -1,21 +1,16 @@ package cmd import ( - set "github.com/deckarep/golang-set" - "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" - "io/ioutil" - "os" - "path" "testing" ) // TestDiffCmdWithAllRequirementsNoError - test diff command parameters // use valid arguments so expect no failures func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { - constellationPathOrg, constellationPathNew, _, _ := localPrepareRealFilesForTest(t) + constellationPathOrg, constellationPathNew, _, _ := helpers.PrepareTwoRealConstellationFilesForTest(t) hook := test.NewGlobal() _, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", constellationPathOrg, "-n", constellationPathNew) @@ -30,32 +25,6 @@ func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { } } -// localPrepareRealFilesForTest - global function assumes only a single input file, this use the two required for the diff command -func localPrepareRealFilesForTest(t *testing.T) (string, string, string, string) { - tdir, err := ioutil.TempDir("./", "output-") - if err != nil { - t.Fatal(err) - } - - defer func() { - err = os.RemoveAll(tdir) - if err != nil { - t.Fatal(err) - } - }() - - cwd, err2 := os.Getwd() - if err2 != nil { - t.Fatal(err2) - } - - constellationPathOrg := path.Join(cwd, "../sample/constellation/sample_constellation.yaml") - constellationPathNew := path.Join(cwd, "../sample/constellation/sample_constellation_changed.yaml") - mapsPath := path.Join(cwd, "../sample/constellation/sample_constellation_maps.yaml") - - return constellationPathOrg, constellationPathNew, mapsPath, tdir -} - // TestDffCmdFailYaml - test diff command parameters // Test both required command line parameters (-o, -n) failing each in turn func TestDffCmdFailYaml(t *testing.T) { @@ -103,180 +72,6 @@ func TestDiffCmdFailNotYaml(t *testing.T) { } } -// TestGetComparisonSets - generate sets of common, added and removed services and relationships from input YAML -// and compare to manually created sets with a known or expected outcome -func TestGetComparisonSets(t *testing.T) { - - dsGraphOrg := new(constellation.Config) - err := dsGraphOrg.LoadString(testOrgDagStr) - assert.NoErrorf(t, err, "dagConfigService failed to load dag from test string %s", err) - - dsGraphNew := new(constellation.Config) - err = dsGraphNew.LoadString(testNewDagStr) - assert.NoErrorf(t, err, "dagConfigService failed to load file %s", err) - - //construct sets struct for loaded constellation - loadedSets := &setsForComparison{} - // will populate with expected/known outcomes - knownSets := &setsForComparison{} - - populateComparisonSets(knownSets) - - // function being tested - fillComparisonSets(dsGraphOrg, dsGraphNew, loadedSets) - - assert.True(t, knownSets.setCommonSvcs.Equal(loadedSets.setCommonSvcs), "Common services - did not match between expected result and input yaml") - assert.True(t, knownSets.setCommonRels.Equal(loadedSets.setCommonRels), "Common relationships - did not match between expected result and input yaml") - assert.True(t, knownSets.setAddedSvcs.Equal(loadedSets.setAddedSvcs), "Added services - did not match between expected result and input yaml") - assert.True(t, knownSets.setAddedRels.Equal(loadedSets.setAddedRels), "Added relationships - did not match between expected result and input yaml") - assert.True(t, knownSets.setDelSvcs.Equal(loadedSets.setDelSvcs), "Deleted services - did not match between expected result and input yaml") - assert.True(t, knownSets.setDelRels.Equal(loadedSets.setDelRels), "Deleted relationships - did not match between expected result and input yaml") -} - -// testGraphWithChanges - test diff comparison function -func TestGraphWithChanges(t *testing.T) { - - dsGraphOrg := new(constellation.Config) - err := dsGraphOrg.LoadString(testOrgDagStr) - if err != nil { - t.Errorf("dagConfigService failed to load dag from test string %s", err) - } - - dsGraphNew := new(constellation.Config) - err = dsGraphNew.LoadString(testNewDagStr) - assert.NoErrorf(t, err, "dagConfigService failed to load file %s", err) - - //construct sets struct for loaded constellation - loadedSets := &setsForComparison{} - - // function being tested - fillComparisonSets(dsGraphOrg, dsGraphNew, loadedSets) - - resString := createGraphWithChanges(dsGraphNew, loadedSets) - - assert.Truef(t, helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString), "Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) -} - -// Utility to populate comparison sets with expected/known result -func populateComparisonSets(target *setsForComparison) { - target.setCommonSvcs = set.NewSet() - target.setCommonSvcs.Add("3aa1e546-1ed5-4d67-a59c-be0d5905b490") - target.setCommonSvcs.Add("1d0255d4-5b8c-4a52-b0bb-ac024cda37e5") - target.setCommonSvcs.Add("a268fae5-2a82-4a3e-ada7-a52eeb7019ac") - - target.setCommonRels = set.NewSet() - target.setCommonRels.Add("3aa1e546-1ed5-4d67-a59c-be0d5905b490" + "|" + "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5") - - target.setAddedSvcs = set.NewSet() - target.setAddedSvcs.Add("9f1bcb3d-ff58-41d4-8779-f71e7b8800f8") - target.setAddedSvcs.Add("b268fae5-2a82-4a3e-ada7-a52eeb7019ac") - - target.setAddedRels = set.NewSet() - target.setAddedRels.Add("9f1bcb3d-ff58-41d4-8779-f71e7b8800f8" + "|" + "3aa1e546-1ed5-4d67-a59c-be0d5905b490") - target.setAddedRels.Add("1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" + "|" + "a268fae5-2a82-4a3e-ada7-a52eeb7019ac") - target.setAddedRels.Add("a268fae5-2a82-4a3e-ada7-a52eeb7019ac" + "|" + "b268fae5-2a82-4a3e-ada7-a52eeb7019ac") - - target.setDelSvcs = set.NewSet() - target.setDelSvcs.Add("9e1bcb3d-ff58-41d4-8779-f71e7b8800f8") - - target.setDelRels = set.NewSet() - target.setDelRels.Add("9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" + "|" + "3aa1e546-1ed5-4d67-a59c-be0d5905b490") - target.setDelRels.Add("3aa1e546-1ed5-4d67-a59c-be0d5905b490" + "|" + "a268fae5-2a82-4a3e-ada7-a52eeb7019ac") -} - -// Sample DAG file data - original file -const testOrgDagStr = ` -Name: "Azure Event Hubs Sample" -Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" -Services: -- Name: "Event Generator" - Id: "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" - Type: "EventGenerator" - Properties: {} -- Name: "Azure Event Hub" - Id: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - Type: "EventHub" - Properties: {} -- Name: "Event Logger" - Id: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" - Type: "EventLogger" - Properties: {} -- Name: "Event Logger" - Id: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" - Type: "EventLogger" - Properties: {} -Relationships: -- Name: "Generator to Event Hubs Link" - Id: "211a55bd-5d92-446c-8be8-190f8f0e623e" - Description: "Event Generator to Event Hub connection" - From: "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" - To: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - Properties: {} -- Name: "Event Hubs to Event Logger Link" - Id: "08ccbd67-456f-4349-854a-4e6959e5017b" - Description: "Event Hubs to Event Logger connection" - From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - To: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" - Properties: {} -- Name: "Event Hubs to Event Logger Link Repeat" - Id: "c8a719e0-164d-408f-9ed1-06e08dc5abbe" - Description: "Event Hubs to Event Logger connection" - From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - To: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" - Properties: {}` - -// An updated (new) constellation dag with known differences -const testNewDagStr = ` -Name: "Azure Event Hubs Sample Changed" -Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" -Services: -- Name: "Event Generator" - Id: "9f1bcb3d-ff58-41d4-8779-f71e7b8800f8" - Type: "EventGenerator" - Properties: {} -- Name: "Azure Event Hub" - Id: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - Type: "EventHub" - Properties: {} -- Name: "Event Logger" - Id: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" - Type: "EventLogger" - Properties: {} -- Name: "Event Logger Added" - Id: "b268fae5-2a82-4a3e-ada7-a52eeb7019ac" - Type: "EventLogger" - Properties: {} -- Name: "Event Logger" - Id: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" - Type: "EventLogger" - Properties: {} -Relationships: -- Name: "Generator to Event Hubs Link" - Id: "211a55bd-5d92-446c-8be8-190f8f0e623e" - Description: "Event Generator to Event Hub connection" - From: "9f1bcb3d-ff58-41d4-8779-f71e7b8800f8" - To: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - Properties: {} -- Name: "Event Hubs to Event Logger Link" - Id: "08ccbd67-456f-4349-854a-4e6959e5017b" - Description: "Event Hubs to Event Logger connection" - From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - To: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" - Properties: {} -- Name: "Event Hubs to Event Logger Link Repeat" - Id: "c8a719e0-164d-408f-9ed1-06e08dc5abbe" - Description: "Event Hubs to Event Logger connection" - From: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" - To: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" - Properties: {} -- Name: "Event Hubs to Event Logger Link Added to the end Repeat" - Id: "d8a719e0-164d-408f-9ed1-06e08dc5abbe" - Description: "Event Hubs to Event Logger connection" - From: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" - To: "b268fae5-2a82-4a3e-ada7-a52eeb7019ac" - Properties: {} - ` - const testDiffComparisonOutputString = `digraph Azure_Event_Hubs_Sample_Changed_diff { rankdir=LR; "9f1bcb3d-ff58-41d4-8779-f71e7b8800f8"->"3aa1e546-1ed5-4d67-a59c-be0d5905b490"[ color="#d8ffa8" ]; diff --git a/internal/platform/constellation/diff.go b/internal/platform/constellation/diff.go new file mode 100644 index 0000000..232b78a --- /dev/null +++ b/internal/platform/constellation/diff.go @@ -0,0 +1,192 @@ +package constellation + +import ( + "fmt" + "github.com/awalterschulze/gographviz" + set "github.com/deckarep/golang-set" + "strings" +) + +// ComparisonSet is a combination of two graphviz graphs. +type ComparisonSet struct { + SetCommonSvcs set.Set + SetCommonRels set.Set + SetAddedSvcs set.Set + SetAddedRels set.Set + SetDelSvcs set.Set + SetDelRels set.Set +} + +//CompareConstellations takes two constellation configurations, compares and returns the differences. +func CompareConstellations(dsOrg *Config, dsNew *Config) (string, error) { + // populate comparison sets with changes between original and new graph + sets := FillComparisonSets(dsOrg, dsNew) + + // build the graphviz output from new graph and comparison sets + return CreateGraphWithChanges(dsNew, &sets) +} + +// FillComparisonSets - loads provided set struct with data from the constellations and then determines the various differences between the +// sets (original constellation and new) to help detemine what has been added, removed or changed. +func FillComparisonSets(dsOrg *Config, dsNew *Config) (sets ComparisonSet) { + setOrgSvcs, setOrgRel := createSet(dsOrg) + setNewSvcs, setNewRel := createSet(dsNew) + + // "Added" means in the new constellation but not the original one + // "Deleted" means something is present in the original constellation but not in the new constellation + // Services + sets.SetCommonSvcs = setOrgSvcs.Intersect(setNewSvcs) //present in both + sets.SetAddedSvcs = setNewSvcs.Difference(setOrgSvcs) //pressent in new but not in original + sets.SetDelSvcs = setOrgSvcs.Difference(setNewSvcs) //present in original but not in new + // Relationships + sets.SetCommonRels = setOrgRel.Intersect(setNewRel) //present in both + sets.SetAddedRels = setNewRel.Difference(setOrgRel) //pressent in new but not in original + sets.SetDelRels = setOrgRel.Difference(setNewRel) //present in original but not in new + + return +} + +// createSet - utility function used to create a pair of result sets (services + relationships) based on an input constellation DAG +func createSet(dsGraph *Config) (set.Set, set.Set) { + + // Create sets to hold services and relationships - used to find differences between old and new using intersection and difference operations + retSetServices := set.NewSet() + retSetRelationships := set.NewSet() + + //Store all services in the services set + for _, v := range dsGraph.Services { + retSetServices.Add(v.ID) + } + + //Store relationships in the relationship set + for _, v := range dsGraph.Relationships { + retSetRelationships.Add(v.From + "|" + v.To) + } + + return retSetServices, retSetRelationships +} + +// CreateGraphWithChanges - use both input constellations (new and original) as well as the comparison sets to create +// a dag that can be visualised. It uses the comparison sets to identify additions, deletions and changes between the original +// and new constellations. +func CreateGraphWithChanges(newGraph *Config, sets *ComparisonSet) (string, error) { + // Lookup is used to map IDs to names. Names are easier to visualise but IDs are more important to ensure the + // presented constellation is correct and IDs are used to link nodes together + lookup := make(map[string]string) + g := gographviz.NewGraph() + + // Replace spaces with underscores, names with spaces can break graphviz engines + if err := g.SetName(strings.Replace(newGraph.Name, " ", "_", -1) + "_diff"); err != nil { + return "", fmt.Errorf("error setting graph name: %v", err) + } + // Attribute in graphviz to change graph orientation - LR indicates Left to Right. Default is top to bottom + if err := g.AddAttr(g.Name, "rankdir", "LR"); err != nil { + return "", fmt.Errorf("error adding node: %v", err) + } + + // Make the graph directed (a constellation is DAG) + if err := g.SetDir(true); err != nil { + return "", fmt.Errorf("error: %v", err) + } + + // Add all services from the new constellation + // - New services - highlight with colour (i.e in setAddedSvcs) + // - Deleted services (i.e. in setDelSvcs) - include and format appropriately + for _, v := range newGraph.Services { + newName := strings.Replace(v.ID, " ", "_", -1) // Replace spaces in names with underscores, names with spaces can break graphviz engines) + + // attributes are graphviz specific that control formatting. The linrary used requires then in the form of a map passed as an argument so it needs + // to be built before adding the item + attrs := make(map[string]string) + + // Check if the service is new to this constellation + if sets.SetAddedSvcs.Contains(newName) { + attrs["color"] = "\"#d8ffa8\"" + } + attrs["label"] = "\"" + v.Type + "\n" + v.ID + "\"" + fillShapeAndStyleForNodeType(v.Type, attrs) + + lookup[v.ID] = newName + err := g.AddNode(newGraph.Name, "\""+newName+"\"", attrs) //Surround names/labels with quotes, stops graphviz seeing special characters and breaking + if err != nil { + return "", fmt.Errorf("error: %v", err) + } + } + + //======================================== process deleted services ========================================== + //Process services that have been removed from the original constellation + for v := range sets.SetDelSvcs.Iter() { + vString, _ := v.(string) + newName := strings.Replace(vString, " ", "_", -1) + attrs := make(map[string]string) + attrs["color"] = "\"#ff9494\"" + fillShapeAndStyleForNodeType("", attrs) + if err := g.AddNode(newGraph.Name, "\""+newName+"\"", attrs); err != nil { + return "", fmt.Errorf("error adding node: %v", err) + } + } + + //======================================== process relationships ========================================== + // Add Relationships from the new constellation + // - New relationships - highlight with colour (i.e. in setAddedSvcs) + // - Deleted relationships (i.e. in setDelRels) - include and format appropriately + for _, v := range newGraph.Relationships { + //Surround names/labels with quotes, stops graphviz seeing special characters and breaking + localFrom := "\"" + lookup[v.From] + "\"" + localTo := "\"" + lookup[v.To] + "\"" + + attrs := make(map[string]string) + // Relationship is stored in the map as source|destination - both are needed to tell if a relationship is new (this is how they are stored in the sets created earlier) + relLookupName := lookup[v.From] + "|" + lookup[v.To] + + // Check if the relationship is new (it will then not be in the common list of relationships between old and new constellation) + if sets.SetAddedRels.Contains(relLookupName) { + attrs["color"] = "\"#d8ffa8\"" + } + + err := g.AddEdge(localFrom, localTo, true, attrs) + if err != nil { + return "", fmt.Errorf("error: %v", err) + } + } + + //======================================== process deleted relationships ===================================== + // Deleted relationships + for v := range sets.SetDelRels.Iter() { + vString := strings.Replace(v.(string), " ", "_", -1) + if len(strings.Split(vString, "|")) != 2 { + return "", fmt.Errorf("Relationships string should be two items separated by | but got %v", vString) + } + newFrom := "\"" + strings.Split(vString, "|")[0] + "\"" + newTo := "\"" + strings.Split(vString, "|")[1] + "\"" + + attrs := make(map[string]string) + attrs["color"] = "\"#ff9494\"" + err := g.AddEdge(newFrom, newTo, true, attrs) + if err != nil { + return "", fmt.Errorf("error: %v", err) + } + } + + // Produce resulting graph in dot notation format + return g.String(), nil +} + +// Populate attrs map with additional attributes based on the node type +// Easier to change in a single place +func fillShapeAndStyleForNodeType(nodeType string, existAttrs map[string]string) { + switch nodeType { + case "EventLogger": + existAttrs["shape"] = "rectangle" + existAttrs["style"] = "\"rounded, filled\"" + case "EventGenerator": + existAttrs["shape"] = "rectangle" + existAttrs["style"] = "\"rounded, filled\"" + case "EventHub": + existAttrs["shape"] = "rectangle" + existAttrs["style"] = "\"rounded, filled\"" + default: + existAttrs["shape"] = "rectangle" + existAttrs["style"] = "\"rounded, filled\"" + } +} diff --git a/internal/platform/constellation/diff_test.go b/internal/platform/constellation/diff_test.go new file mode 100644 index 0000000..1822473 --- /dev/null +++ b/internal/platform/constellation/diff_test.go @@ -0,0 +1,109 @@ +package constellation_test + +import ( + set "github.com/deckarep/golang-set" + "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/stretchr/testify/assert" + "testing" +) + +// TestGetComparisonSets - generate sets of common, added and removed services and relationships from input YAML +// and compare to manually created sets with a known or expected outcome +func TestGetComparisonSets(t *testing.T) { + + dsGraphOrg := new(constellation.Config) + err := dsGraphOrg.LoadFile("testdata/diff/original.yaml") + assert.NoErrorf(t, err, "dagConfigService failed to load dag from test string %s", err) + + dsGraphNew := new(constellation.Config) + err = dsGraphNew.LoadFile("testdata/diff/modified.yaml") + assert.NoErrorf(t, err, "dagConfigService failed to load file %s", err) + + // will populate with expected/known outcomes + knownSets := &constellation.ComparisonSet{} + + populateComparisonSets(knownSets) + + // function being tested + loadedSets := constellation.FillComparisonSets(dsGraphOrg, dsGraphNew) + + assert.True(t, knownSets.SetCommonSvcs.Equal(loadedSets.SetCommonSvcs), "Common services - did not match between expected result and input yaml") + assert.True(t, knownSets.SetCommonRels.Equal(loadedSets.SetCommonRels), "Common relationships - did not match between expected result and input yaml") + assert.True(t, knownSets.SetAddedSvcs.Equal(loadedSets.SetAddedSvcs), "Added services - did not match between expected result and input yaml") + assert.True(t, knownSets.SetAddedRels.Equal(loadedSets.SetAddedRels), "Added relationships - did not match between expected result and input yaml") + assert.True(t, knownSets.SetDelSvcs.Equal(loadedSets.SetDelSvcs), "Deleted services - did not match between expected result and input yaml") + assert.True(t, knownSets.SetDelRels.Equal(loadedSets.SetDelRels), "Deleted relationships - did not match between expected result and input yaml") +} + +// testGraphWithChanges - test diff comparison function +func TestGraphWithChanges(t *testing.T) { + + dsGraphOrg := new(constellation.Config) + err := dsGraphOrg.LoadFile("testdata/diff/original.yaml") + if err != nil { + t.Errorf("dagConfigService failed to load dag from test string %s", err) + } + + dsGraphNew := new(constellation.Config) + err = dsGraphNew.LoadFile("testdata/diff/modified.yaml") + assert.NoErrorf(t, err, "dagConfigService failed to load file %s", err) + + // function being tested + loadedSets := constellation.FillComparisonSets(dsGraphOrg, dsGraphNew) + + resString, err := constellation.CreateGraphWithChanges(dsGraphNew, &loadedSets) + + assert.NoError(t, err) + assert.Truef(t, helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString), "Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) +} + +// Utility to populate comparison sets with expected/known result +func populateComparisonSets(target *constellation.ComparisonSet) { + target.SetCommonSvcs = set.NewSet() + target.SetCommonSvcs.Add("3aa1e546-1ed5-4d67-a59c-be0d5905b490") + target.SetCommonSvcs.Add("1d0255d4-5b8c-4a52-b0bb-ac024cda37e5") + target.SetCommonSvcs.Add("a268fae5-2a82-4a3e-ada7-a52eeb7019ac") + + target.SetCommonRels = set.NewSet() + target.SetCommonRels.Add("3aa1e546-1ed5-4d67-a59c-be0d5905b490" + "|" + "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5") + + target.SetAddedSvcs = set.NewSet() + target.SetAddedSvcs.Add("9f1bcb3d-ff58-41d4-8779-f71e7b8800f8") + target.SetAddedSvcs.Add("b268fae5-2a82-4a3e-ada7-a52eeb7019ac") + + target.SetAddedRels = set.NewSet() + target.SetAddedRels.Add("9f1bcb3d-ff58-41d4-8779-f71e7b8800f8" + "|" + "3aa1e546-1ed5-4d67-a59c-be0d5905b490") + target.SetAddedRels.Add("1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" + "|" + "a268fae5-2a82-4a3e-ada7-a52eeb7019ac") + target.SetAddedRels.Add("a268fae5-2a82-4a3e-ada7-a52eeb7019ac" + "|" + "b268fae5-2a82-4a3e-ada7-a52eeb7019ac") + + target.SetDelSvcs = set.NewSet() + target.SetDelSvcs.Add("9e1bcb3d-ff58-41d4-8779-f71e7b8800f8") + + target.SetDelRels = set.NewSet() + target.SetDelRels.Add("9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" + "|" + "3aa1e546-1ed5-4d67-a59c-be0d5905b490") + target.SetDelRels.Add("3aa1e546-1ed5-4d67-a59c-be0d5905b490" + "|" + "a268fae5-2a82-4a3e-ada7-a52eeb7019ac") +} + +const testDiffComparisonOutputString = `digraph Azure_Event_Hubs_Sample_Changed_diff { + rankdir=LR; + "9f1bcb3d-ff58-41d4-8779-f71e7b8800f8"->"3aa1e546-1ed5-4d67-a59c-be0d5905b490"[ color="#d8ffa8" ]; + "3aa1e546-1ed5-4d67-a59c-be0d5905b490"->"1d0255d4-5b8c-4a52-b0bb-ac024cda37e5"; + "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5"->"a268fae5-2a82-4a3e-ada7-a52eeb7019ac"[ color="#d8ffa8" ]; + "a268fae5-2a82-4a3e-ada7-a52eeb7019ac"->"b268fae5-2a82-4a3e-ada7-a52eeb7019ac"[ color="#d8ffa8" ]; + "3aa1e546-1ed5-4d67-a59c-be0d5905b490"->"a268fae5-2a82-4a3e-ada7-a52eeb7019ac"[ color="#ff9494" ]; + "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8"->"3aa1e546-1ed5-4d67-a59c-be0d5905b490"[ color="#ff9494" ]; + "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" [ label="EventLogger +1d0255d4-5b8c-4a52-b0bb-ac024cda37e5", shape=rectangle, style="rounded, filled" ]; + "3aa1e546-1ed5-4d67-a59c-be0d5905b490" [ label="EventHub +3aa1e546-1ed5-4d67-a59c-be0d5905b490", shape=rectangle, style="rounded, filled" ]; + "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" [ color="#ff9494", shape=rectangle, style="rounded, filled" ]; + "9f1bcb3d-ff58-41d4-8779-f71e7b8800f8" [ color="#d8ffa8", label="EventGenerator +9f1bcb3d-ff58-41d4-8779-f71e7b8800f8", shape=rectangle, style="rounded, filled" ]; + "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" [ label="EventLogger +a268fae5-2a82-4a3e-ada7-a52eeb7019ac", shape=rectangle, style="rounded, filled" ]; + "b268fae5-2a82-4a3e-ada7-a52eeb7019ac" [ color="#d8ffa8", label="EventLogger +b268fae5-2a82-4a3e-ada7-a52eeb7019ac", shape=rectangle, style="rounded, filled" ]; + +} +` diff --git a/internal/platform/constellation/testdata/diff/modified.yaml b/internal/platform/constellation/testdata/diff/modified.yaml new file mode 100644 index 0000000..cbd181e --- /dev/null +++ b/internal/platform/constellation/testdata/diff/modified.yaml @@ -0,0 +1,48 @@ +Name: "Azure Event Hubs Sample Changed" +Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" +Services: +- Name: "Event Generator" + Id: "9f1bcb3d-ff58-41d4-8779-f71e7b8800f8" + Type: "EventGenerator" + Properties: {} +- Name: "Azure Event Hub" + Id: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + Type: "EventHub" + Properties: {} +- Name: "Event Logger" + Id: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" + Type: "EventLogger" + Properties: {} +- Name: "Event Logger Added" + Id: "b268fae5-2a82-4a3e-ada7-a52eeb7019ac" + Type: "EventLogger" + Properties: {} +- Name: "Event Logger" + Id: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" + Type: "EventLogger" + Properties: {} +Relationships: +- Name: "Generator to Event Hubs Link" + Id: "211a55bd-5d92-446c-8be8-190f8f0e623e" + Description: "Event Generator to Event Hub connection" + From: "9f1bcb3d-ff58-41d4-8779-f71e7b8800f8" + To: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + Properties: {} +- Name: "Event Hubs to Event Logger Link" + Id: "08ccbd67-456f-4349-854a-4e6959e5017b" + Description: "Event Hubs to Event Logger connection" + From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + To: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" + Properties: {} +- Name: "Event Hubs to Event Logger Link Repeat" + Id: "c8a719e0-164d-408f-9ed1-06e08dc5abbe" + Description: "Event Hubs to Event Logger connection" + From: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" + To: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" + Properties: {} +- Name: "Event Hubs to Event Logger Link Added to the end Repeat" + Id: "d8a719e0-164d-408f-9ed1-06e08dc5abbe" + Description: "Event Hubs to Event Logger connection" + From: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" + To: "b268fae5-2a82-4a3e-ada7-a52eeb7019ac" + Properties: {} diff --git a/internal/platform/constellation/testdata/diff/original.yaml b/internal/platform/constellation/testdata/diff/original.yaml new file mode 100644 index 0000000..493c280 --- /dev/null +++ b/internal/platform/constellation/testdata/diff/original.yaml @@ -0,0 +1,38 @@ +Name: "Azure Event Hubs Sample" +Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" +Services: +- Name: "Event Generator" + Id: "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" + Type: "EventGenerator" + Properties: {} +- Name: "Azure Event Hub" + Id: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + Type: "EventHub" + Properties: {} +- Name: "Event Logger" + Id: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" + Type: "EventLogger" + Properties: {} +- Name: "Event Logger" + Id: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" + Type: "EventLogger" + Properties: {} +Relationships: +- Name: "Generator to Event Hubs Link" + Id: "211a55bd-5d92-446c-8be8-190f8f0e623e" + Description: "Event Generator to Event Hub connection" + From: "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" + To: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + Properties: {} +- Name: "Event Hubs to Event Logger Link" + Id: "08ccbd67-456f-4349-854a-4e6959e5017b" + Description: "Event Hubs to Event Logger connection" + From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + To: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" + Properties: {} +- Name: "Event Hubs to Event Logger Link Repeat" + Id: "c8a719e0-164d-408f-9ed1-06e08dc5abbe" + Description: "Event Hubs to Event Logger connection" + From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + To: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" + Properties: {} diff --git a/internal/tools/helpers/test.go b/internal/tools/helpers/test.go index 3e4fe09..9b1a949 100644 --- a/internal/tools/helpers/test.go +++ b/internal/tools/helpers/test.go @@ -50,6 +50,32 @@ func PrepareRealFilesForTest(t *testing.T) (string, string, string) { return constellationPath, mapsPath, tdir } +// PrepareTwoRealConstellationFilesForTest - this use the two required for the diff command +func PrepareTwoRealConstellationFilesForTest(t *testing.T) (string, string, string, string) { + tdir, err := ioutil.TempDir("./", "output-") + if err != nil { + t.Fatal(err) + } + + defer func() { + err = os.RemoveAll(tdir) + if err != nil { + t.Fatal(err) + } + }() + + cwd, err2 := os.Getwd() + if err2 != nil { + t.Fatal(err2) + } + + constellationPathOrg := path.Join(cwd, "../sample/constellation/sample_constellation.yaml") + constellationPathNew := path.Join(cwd, "../sample/constellation/sample_constellation_changed.yaml") + mapsPath := path.Join(cwd, "../sample/constellation/sample_constellation_maps.yaml") + + return constellationPathOrg, constellationPathNew, mapsPath, tdir +} + func CleanTempTestFiles(t *testing.T, temp string) { err := os.RemoveAll(temp) if err != nil { From b73d301595562e617074c5c611ad786e857e35b6 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 15 Jan 2020 11:18:28 +1100 Subject: [PATCH 07/85] Fixed compose_test. --- internal/compose/compose_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go index f99c01f..657d65f 100644 --- a/internal/compose/compose_test.go +++ b/internal/compose/compose_test.go @@ -29,7 +29,7 @@ func TestComposeService(t *testing.T) { comp := new(compose.Composer) _, err = comp.Build("test", tdir) - assert.NoError(t, err, "Compose should fail if not yet loaded") + assert.Error(t, err, "Compose should fail if not yet loaded") _ = comp.LoadString(test01DagStr, configMapTest01String) From 951f53a6f68ba1244024a58824cf874779178da1 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 15 Jan 2020 13:09:21 +1100 Subject: [PATCH 08/85] Renamed .scripts to scripts and sample to examples. --- README.md | 2 +- cmd/diff_test.go | 4 ++-- docs/usage.md | 6 +++--- .../constellation/http_constellation.yaml | 0 .../constellation/http_constellation_maps.yaml | 4 ++-- .../constellation/sample_constellation.yaml | 0 .../constellation/sample_constellation_changed.yaml | 0 .../constellation/sample_constellation_maps.yaml | 6 +++--- .../deps/event_hub_sample_event_generator/.helmignore | 0 .../deps/event_hub_sample_event_generator/Chart.yaml | 0 .../templates/NOTES.txt | 0 .../templates/_helpers.tpl | 0 .../templates/deployment.yaml | 0 .../templates/ingress.yaml | 0 .../templates/service.yaml | 0 .../templates/tests/test-connection.yaml | 0 .../deps/event_hub_sample_event_generator/values.yaml | 0 .../deps/event_hub_sample_event_hub/.helmignore | 0 .../deps/event_hub_sample_event_hub/Chart.yaml | 0 .../event_hub_sample_event_hub/templates/NOTES.txt | 0 .../event_hub_sample_event_hub/templates/_helpers.tpl | 0 .../templates/deployment.yaml | 0 .../event_hub_sample_event_hub/templates/ingress.yaml | 0 .../event_hub_sample_event_hub/templates/service.yaml | 0 .../templates/tests/test-connection.yaml | 0 .../deps/event_hub_sample_event_hub/values.yaml | 0 .../deps/event_hub_sample_event_logger/.helmignore | 0 .../deps/event_hub_sample_event_logger/Chart.yaml | 0 .../event_hub_sample_event_logger/templates/NOTES.txt | 0 .../templates/_helpers.tpl | 0 .../templates/deployment.yaml | 0 .../templates/ingress.yaml | 0 .../templates/service.yaml | 0 .../templates/tests/test-connection.yaml | 0 .../deps/event_hub_sample_event_logger/values.yaml | 0 {sample => examples}/helm/basictest/Chart.yaml | 8 ++++---- .../helm/basictest/templates/NOTES.txt | 0 .../helm/basictest/templates/_helpers.tpl | 0 .../helm/basictest/templates/deployment.yaml | 0 .../helm/basictest/templates/ingress.yaml | 0 .../helm/basictest/templates/service.yaml | 0 .../helm/basictest/templates/serviceaccount.yaml | 0 .../basictest/templates/tests/test-connection.yaml | 0 {sample => examples}/helm/basictest/values.yaml | 0 .../helm/http_sample/receiver/Chart.yaml | 0 .../helm/http_sample/receiver/templates/NOTES.txt | 0 .../http_sample/receiver/templates/receiverapp.yaml | 0 .../receiver/templates/receiverservice.yaml | 0 .../helm/http_sample/receiver/values.yaml | 0 .../helm/http_sample/sender/Chart.yaml | 0 .../helm/http_sample/sender/templates/NOTES.txt | 0 .../helm/http_sample/sender/templates/senderapp.yaml | 0 .../http_sample/sender/templates/senderservice.yaml | 0 .../helm/http_sample/sender/values.yaml | 0 internal/tools/helpers/test.go | 10 +++++----- makefile | 10 +++++----- {.scripts => scripts}/rbac.yaml | 0 {.scripts => scripts}/test_watcher.sh | 4 +--- 58 files changed, 26 insertions(+), 28 deletions(-) rename {sample => examples}/constellation/http_constellation.yaml (100%) rename {sample => examples}/constellation/http_constellation_maps.yaml (63%) rename {sample => examples}/constellation/sample_constellation.yaml (100%) rename {sample => examples}/constellation/sample_constellation_changed.yaml (100%) rename {sample => examples}/constellation/sample_constellation_maps.yaml (60%) rename {sample => examples}/deps/event_hub_sample_event_generator/.helmignore (100%) rename {sample => examples}/deps/event_hub_sample_event_generator/Chart.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_generator/templates/NOTES.txt (100%) rename {sample => examples}/deps/event_hub_sample_event_generator/templates/_helpers.tpl (100%) rename {sample => examples}/deps/event_hub_sample_event_generator/templates/deployment.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_generator/templates/ingress.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_generator/templates/service.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_generator/values.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_hub/.helmignore (100%) rename {sample => examples}/deps/event_hub_sample_event_hub/Chart.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_hub/templates/NOTES.txt (100%) rename {sample => examples}/deps/event_hub_sample_event_hub/templates/_helpers.tpl (100%) rename {sample => examples}/deps/event_hub_sample_event_hub/templates/deployment.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_hub/templates/ingress.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_hub/templates/service.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_hub/values.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_logger/.helmignore (100%) rename {sample => examples}/deps/event_hub_sample_event_logger/Chart.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_logger/templates/NOTES.txt (100%) rename {sample => examples}/deps/event_hub_sample_event_logger/templates/_helpers.tpl (100%) rename {sample => examples}/deps/event_hub_sample_event_logger/templates/deployment.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_logger/templates/ingress.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_logger/templates/service.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml (100%) rename {sample => examples}/deps/event_hub_sample_event_logger/values.yaml (100%) rename {sample => examples}/helm/basictest/Chart.yaml (55%) rename {sample => examples}/helm/basictest/templates/NOTES.txt (100%) rename {sample => examples}/helm/basictest/templates/_helpers.tpl (100%) rename {sample => examples}/helm/basictest/templates/deployment.yaml (100%) rename {sample => examples}/helm/basictest/templates/ingress.yaml (100%) rename {sample => examples}/helm/basictest/templates/service.yaml (100%) rename {sample => examples}/helm/basictest/templates/serviceaccount.yaml (100%) rename {sample => examples}/helm/basictest/templates/tests/test-connection.yaml (100%) rename {sample => examples}/helm/basictest/values.yaml (100%) rename {sample => examples}/helm/http_sample/receiver/Chart.yaml (100%) rename {sample => examples}/helm/http_sample/receiver/templates/NOTES.txt (100%) rename {sample => examples}/helm/http_sample/receiver/templates/receiverapp.yaml (100%) rename {sample => examples}/helm/http_sample/receiver/templates/receiverservice.yaml (100%) rename {sample => examples}/helm/http_sample/receiver/values.yaml (100%) rename {sample => examples}/helm/http_sample/sender/Chart.yaml (100%) rename {sample => examples}/helm/http_sample/sender/templates/NOTES.txt (100%) rename {sample => examples}/helm/http_sample/sender/templates/senderapp.yaml (100%) rename {sample => examples}/helm/http_sample/sender/templates/senderservice.yaml (100%) rename {sample => examples}/helm/http_sample/sender/values.yaml (100%) rename {.scripts => scripts}/rbac.yaml (100%) rename {.scripts => scripts}/test_watcher.sh (89%) diff --git a/README.md b/README.md index daa402f..0305d87 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ With everything installed and running, you can continue. You can find sample constellation files in the following location: -`/sample/constellation/` +`/examples/constellation/` Using these files you can test the app binary. diff --git a/cmd/diff_test.go b/cmd/diff_test.go index 74bb849..288fcf5 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -40,7 +40,7 @@ func TestDffCmdFailYaml(t *testing.T) { expected = "Could not open new YAML input file for reading constellationPathNew" - output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../sample/constellation/sample_constellation.yaml", "-n", "constellationPathNew") + output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "constellationPathNew") if err != nil { helpers.CheckStringContains(t, err.Error(), expected) @@ -63,7 +63,7 @@ func TestDiffCmdFailNotYaml(t *testing.T) { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } - output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../sample/constellation/sample_constellation.yaml", "-n", "diff.go") + output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "diff.go") if err != nil { helpers.CheckStringContains(t, err.Error(), expected) diff --git a/docs/usage.md b/docs/usage.md index 8d38939..5fb33a1 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -54,12 +54,12 @@ Can compose a Helm chart directory (default) or a __.tgz__ of the produced helm Create a Helm chart named `http-demo` to be generated under ./output. ```bash -./abstrakt compose http-demo -f ./sample/constellation/http_constellation.yaml -m ./sample/constellation/http_constellation_maps.yaml -o ./output/http-demo +./abstrakt compose http-demo -f ./examples/constellation/http_constellation.yaml -m ./examples/constellation/http_constellation_maps.yaml -o ./output/http-demo ``` With __.tgz__ ```bash -./abstrakt compose http-demo -f ./sample/constellation/http_constellation.yaml -m ./sample/constellation/http_constellation_maps.yaml -o ./output/http-demo -z +./abstrakt compose http-demo -f ./examples/constellation/http_constellation.yaml -m ./examples/constellation/http_constellation_maps.yaml -o ./output/http-demo -z ``` ### abstrakt `validate` @@ -143,4 +143,4 @@ Run visualise on a file Pipe visualise output to Graphviz producing a file called result.png (assumes Graphviz is installed and can be called from the location abstrakt is being run) - abstrakt visualise -f ./sample/constellation/sample_consteallation.yaml | dot -Tpng > result.png \ No newline at end of file + abstrakt visualise -f ./examples/constellation/sample_consteallation.yaml | dot -Tpng > result.png \ No newline at end of file diff --git a/sample/constellation/http_constellation.yaml b/examples/constellation/http_constellation.yaml similarity index 100% rename from sample/constellation/http_constellation.yaml rename to examples/constellation/http_constellation.yaml diff --git a/sample/constellation/http_constellation_maps.yaml b/examples/constellation/http_constellation_maps.yaml similarity index 63% rename from sample/constellation/http_constellation_maps.yaml rename to examples/constellation/http_constellation_maps.yaml index b8d8f41..e063044 100644 --- a/sample/constellation/http_constellation_maps.yaml +++ b/examples/constellation/http_constellation_maps.yaml @@ -3,9 +3,9 @@ Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" Maps: - ChartName: "sender" Type: "WormholeSender" - Location: "file://../../../sample/helm/http_sample/sender" + Location: "file://../../../examples/helm/http_sample/sender" Version: "0.0.1" - ChartName: "receiver" Type: "WormholeReceiver" - Location: "file://../../../sample/helm/http_sample/receiver" + Location: "file://../../../examples/helm/http_sample/receiver" Version: "0.0.1" diff --git a/sample/constellation/sample_constellation.yaml b/examples/constellation/sample_constellation.yaml similarity index 100% rename from sample/constellation/sample_constellation.yaml rename to examples/constellation/sample_constellation.yaml diff --git a/sample/constellation/sample_constellation_changed.yaml b/examples/constellation/sample_constellation_changed.yaml similarity index 100% rename from sample/constellation/sample_constellation_changed.yaml rename to examples/constellation/sample_constellation_changed.yaml diff --git a/sample/constellation/sample_constellation_maps.yaml b/examples/constellation/sample_constellation_maps.yaml similarity index 60% rename from sample/constellation/sample_constellation_maps.yaml rename to examples/constellation/sample_constellation_maps.yaml index 5ba7e34..7c55248 100644 --- a/sample/constellation/sample_constellation_maps.yaml +++ b/examples/constellation/sample_constellation_maps.yaml @@ -3,14 +3,14 @@ Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" Maps: - ChartName: "event_hub_sample_event_generator" Type: "EventGenerator" - Location: "file://../../../sample/deps/event_hub_sample_event_generator" + Location: "file://../../../examples/deps/event_hub_sample_event_generator" Version: "1.0.0" - ChartName: "event_hub_sample_event_logger" Type: "EventLogger" - Location: "file://../../../sample/deps/event_hub_sample_event_logger" + Location: "file://../../../examples/deps/event_hub_sample_event_logger" Version: "1.0.0" - ChartName: "event_hub_sample_event_hub" Type: "EventHub" - Location: "file://../../../sample/deps/event_hub_sample_event_hub" + Location: "file://../../../examples/deps/event_hub_sample_event_hub" Version: "1.0.0" \ No newline at end of file diff --git a/sample/deps/event_hub_sample_event_generator/.helmignore b/examples/deps/event_hub_sample_event_generator/.helmignore similarity index 100% rename from sample/deps/event_hub_sample_event_generator/.helmignore rename to examples/deps/event_hub_sample_event_generator/.helmignore diff --git a/sample/deps/event_hub_sample_event_generator/Chart.yaml b/examples/deps/event_hub_sample_event_generator/Chart.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_generator/Chart.yaml rename to examples/deps/event_hub_sample_event_generator/Chart.yaml diff --git a/sample/deps/event_hub_sample_event_generator/templates/NOTES.txt b/examples/deps/event_hub_sample_event_generator/templates/NOTES.txt similarity index 100% rename from sample/deps/event_hub_sample_event_generator/templates/NOTES.txt rename to examples/deps/event_hub_sample_event_generator/templates/NOTES.txt diff --git a/sample/deps/event_hub_sample_event_generator/templates/_helpers.tpl b/examples/deps/event_hub_sample_event_generator/templates/_helpers.tpl similarity index 100% rename from sample/deps/event_hub_sample_event_generator/templates/_helpers.tpl rename to examples/deps/event_hub_sample_event_generator/templates/_helpers.tpl diff --git a/sample/deps/event_hub_sample_event_generator/templates/deployment.yaml b/examples/deps/event_hub_sample_event_generator/templates/deployment.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_generator/templates/deployment.yaml rename to examples/deps/event_hub_sample_event_generator/templates/deployment.yaml diff --git a/sample/deps/event_hub_sample_event_generator/templates/ingress.yaml b/examples/deps/event_hub_sample_event_generator/templates/ingress.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_generator/templates/ingress.yaml rename to examples/deps/event_hub_sample_event_generator/templates/ingress.yaml diff --git a/sample/deps/event_hub_sample_event_generator/templates/service.yaml b/examples/deps/event_hub_sample_event_generator/templates/service.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_generator/templates/service.yaml rename to examples/deps/event_hub_sample_event_generator/templates/service.yaml diff --git a/sample/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml b/examples/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml rename to examples/deps/event_hub_sample_event_generator/templates/tests/test-connection.yaml diff --git a/sample/deps/event_hub_sample_event_generator/values.yaml b/examples/deps/event_hub_sample_event_generator/values.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_generator/values.yaml rename to examples/deps/event_hub_sample_event_generator/values.yaml diff --git a/sample/deps/event_hub_sample_event_hub/.helmignore b/examples/deps/event_hub_sample_event_hub/.helmignore similarity index 100% rename from sample/deps/event_hub_sample_event_hub/.helmignore rename to examples/deps/event_hub_sample_event_hub/.helmignore diff --git a/sample/deps/event_hub_sample_event_hub/Chart.yaml b/examples/deps/event_hub_sample_event_hub/Chart.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_hub/Chart.yaml rename to examples/deps/event_hub_sample_event_hub/Chart.yaml diff --git a/sample/deps/event_hub_sample_event_hub/templates/NOTES.txt b/examples/deps/event_hub_sample_event_hub/templates/NOTES.txt similarity index 100% rename from sample/deps/event_hub_sample_event_hub/templates/NOTES.txt rename to examples/deps/event_hub_sample_event_hub/templates/NOTES.txt diff --git a/sample/deps/event_hub_sample_event_hub/templates/_helpers.tpl b/examples/deps/event_hub_sample_event_hub/templates/_helpers.tpl similarity index 100% rename from sample/deps/event_hub_sample_event_hub/templates/_helpers.tpl rename to examples/deps/event_hub_sample_event_hub/templates/_helpers.tpl diff --git a/sample/deps/event_hub_sample_event_hub/templates/deployment.yaml b/examples/deps/event_hub_sample_event_hub/templates/deployment.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_hub/templates/deployment.yaml rename to examples/deps/event_hub_sample_event_hub/templates/deployment.yaml diff --git a/sample/deps/event_hub_sample_event_hub/templates/ingress.yaml b/examples/deps/event_hub_sample_event_hub/templates/ingress.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_hub/templates/ingress.yaml rename to examples/deps/event_hub_sample_event_hub/templates/ingress.yaml diff --git a/sample/deps/event_hub_sample_event_hub/templates/service.yaml b/examples/deps/event_hub_sample_event_hub/templates/service.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_hub/templates/service.yaml rename to examples/deps/event_hub_sample_event_hub/templates/service.yaml diff --git a/sample/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml b/examples/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml rename to examples/deps/event_hub_sample_event_hub/templates/tests/test-connection.yaml diff --git a/sample/deps/event_hub_sample_event_hub/values.yaml b/examples/deps/event_hub_sample_event_hub/values.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_hub/values.yaml rename to examples/deps/event_hub_sample_event_hub/values.yaml diff --git a/sample/deps/event_hub_sample_event_logger/.helmignore b/examples/deps/event_hub_sample_event_logger/.helmignore similarity index 100% rename from sample/deps/event_hub_sample_event_logger/.helmignore rename to examples/deps/event_hub_sample_event_logger/.helmignore diff --git a/sample/deps/event_hub_sample_event_logger/Chart.yaml b/examples/deps/event_hub_sample_event_logger/Chart.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_logger/Chart.yaml rename to examples/deps/event_hub_sample_event_logger/Chart.yaml diff --git a/sample/deps/event_hub_sample_event_logger/templates/NOTES.txt b/examples/deps/event_hub_sample_event_logger/templates/NOTES.txt similarity index 100% rename from sample/deps/event_hub_sample_event_logger/templates/NOTES.txt rename to examples/deps/event_hub_sample_event_logger/templates/NOTES.txt diff --git a/sample/deps/event_hub_sample_event_logger/templates/_helpers.tpl b/examples/deps/event_hub_sample_event_logger/templates/_helpers.tpl similarity index 100% rename from sample/deps/event_hub_sample_event_logger/templates/_helpers.tpl rename to examples/deps/event_hub_sample_event_logger/templates/_helpers.tpl diff --git a/sample/deps/event_hub_sample_event_logger/templates/deployment.yaml b/examples/deps/event_hub_sample_event_logger/templates/deployment.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_logger/templates/deployment.yaml rename to examples/deps/event_hub_sample_event_logger/templates/deployment.yaml diff --git a/sample/deps/event_hub_sample_event_logger/templates/ingress.yaml b/examples/deps/event_hub_sample_event_logger/templates/ingress.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_logger/templates/ingress.yaml rename to examples/deps/event_hub_sample_event_logger/templates/ingress.yaml diff --git a/sample/deps/event_hub_sample_event_logger/templates/service.yaml b/examples/deps/event_hub_sample_event_logger/templates/service.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_logger/templates/service.yaml rename to examples/deps/event_hub_sample_event_logger/templates/service.yaml diff --git a/sample/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml b/examples/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml rename to examples/deps/event_hub_sample_event_logger/templates/tests/test-connection.yaml diff --git a/sample/deps/event_hub_sample_event_logger/values.yaml b/examples/deps/event_hub_sample_event_logger/values.yaml similarity index 100% rename from sample/deps/event_hub_sample_event_logger/values.yaml rename to examples/deps/event_hub_sample_event_logger/values.yaml diff --git a/sample/helm/basictest/Chart.yaml b/examples/helm/basictest/Chart.yaml similarity index 55% rename from sample/helm/basictest/Chart.yaml rename to examples/helm/basictest/Chart.yaml index f89c469..7acc775 100644 --- a/sample/helm/basictest/Chart.yaml +++ b/examples/helm/basictest/Chart.yaml @@ -2,17 +2,17 @@ apiVersion: v2 appVersion: 1.16.0 dependencies: - name: event_hub_sample_event_generator - repository: "file://../../../../sample/deps/event_hub_sample_event_generator" + repository: "file://../../../../examples/deps/event_hub_sample_event_generator" version: 1.0.0 - name: event_hub_sample_event_hub - repository: "file://../../../../sample/deps/event_hub_sample_event_hub" + repository: "file://../../../../examples/deps/event_hub_sample_event_hub" version: 1.0.0 - name: event_hub_sample_event_logger - repository: "file://../../../../sample/deps/event_hub_sample_event_logger" + repository: "file://../../../../examples/deps/event_hub_sample_event_logger" version: 1.0.0 - alias: event_hub_sample_event_logger1 name: event_hub_sample_event_logger - repository: "file://../../../../sample/deps/event_hub_sample_event_logger" + repository: "file://../../../../examples/deps/event_hub_sample_event_logger" version: 1.0.0 description: A Helm chart for Kubernetes name: test diff --git a/sample/helm/basictest/templates/NOTES.txt b/examples/helm/basictest/templates/NOTES.txt similarity index 100% rename from sample/helm/basictest/templates/NOTES.txt rename to examples/helm/basictest/templates/NOTES.txt diff --git a/sample/helm/basictest/templates/_helpers.tpl b/examples/helm/basictest/templates/_helpers.tpl similarity index 100% rename from sample/helm/basictest/templates/_helpers.tpl rename to examples/helm/basictest/templates/_helpers.tpl diff --git a/sample/helm/basictest/templates/deployment.yaml b/examples/helm/basictest/templates/deployment.yaml similarity index 100% rename from sample/helm/basictest/templates/deployment.yaml rename to examples/helm/basictest/templates/deployment.yaml diff --git a/sample/helm/basictest/templates/ingress.yaml b/examples/helm/basictest/templates/ingress.yaml similarity index 100% rename from sample/helm/basictest/templates/ingress.yaml rename to examples/helm/basictest/templates/ingress.yaml diff --git a/sample/helm/basictest/templates/service.yaml b/examples/helm/basictest/templates/service.yaml similarity index 100% rename from sample/helm/basictest/templates/service.yaml rename to examples/helm/basictest/templates/service.yaml diff --git a/sample/helm/basictest/templates/serviceaccount.yaml b/examples/helm/basictest/templates/serviceaccount.yaml similarity index 100% rename from sample/helm/basictest/templates/serviceaccount.yaml rename to examples/helm/basictest/templates/serviceaccount.yaml diff --git a/sample/helm/basictest/templates/tests/test-connection.yaml b/examples/helm/basictest/templates/tests/test-connection.yaml similarity index 100% rename from sample/helm/basictest/templates/tests/test-connection.yaml rename to examples/helm/basictest/templates/tests/test-connection.yaml diff --git a/sample/helm/basictest/values.yaml b/examples/helm/basictest/values.yaml similarity index 100% rename from sample/helm/basictest/values.yaml rename to examples/helm/basictest/values.yaml diff --git a/sample/helm/http_sample/receiver/Chart.yaml b/examples/helm/http_sample/receiver/Chart.yaml similarity index 100% rename from sample/helm/http_sample/receiver/Chart.yaml rename to examples/helm/http_sample/receiver/Chart.yaml diff --git a/sample/helm/http_sample/receiver/templates/NOTES.txt b/examples/helm/http_sample/receiver/templates/NOTES.txt similarity index 100% rename from sample/helm/http_sample/receiver/templates/NOTES.txt rename to examples/helm/http_sample/receiver/templates/NOTES.txt diff --git a/sample/helm/http_sample/receiver/templates/receiverapp.yaml b/examples/helm/http_sample/receiver/templates/receiverapp.yaml similarity index 100% rename from sample/helm/http_sample/receiver/templates/receiverapp.yaml rename to examples/helm/http_sample/receiver/templates/receiverapp.yaml diff --git a/sample/helm/http_sample/receiver/templates/receiverservice.yaml b/examples/helm/http_sample/receiver/templates/receiverservice.yaml similarity index 100% rename from sample/helm/http_sample/receiver/templates/receiverservice.yaml rename to examples/helm/http_sample/receiver/templates/receiverservice.yaml diff --git a/sample/helm/http_sample/receiver/values.yaml b/examples/helm/http_sample/receiver/values.yaml similarity index 100% rename from sample/helm/http_sample/receiver/values.yaml rename to examples/helm/http_sample/receiver/values.yaml diff --git a/sample/helm/http_sample/sender/Chart.yaml b/examples/helm/http_sample/sender/Chart.yaml similarity index 100% rename from sample/helm/http_sample/sender/Chart.yaml rename to examples/helm/http_sample/sender/Chart.yaml diff --git a/sample/helm/http_sample/sender/templates/NOTES.txt b/examples/helm/http_sample/sender/templates/NOTES.txt similarity index 100% rename from sample/helm/http_sample/sender/templates/NOTES.txt rename to examples/helm/http_sample/sender/templates/NOTES.txt diff --git a/sample/helm/http_sample/sender/templates/senderapp.yaml b/examples/helm/http_sample/sender/templates/senderapp.yaml similarity index 100% rename from sample/helm/http_sample/sender/templates/senderapp.yaml rename to examples/helm/http_sample/sender/templates/senderapp.yaml diff --git a/sample/helm/http_sample/sender/templates/senderservice.yaml b/examples/helm/http_sample/sender/templates/senderservice.yaml similarity index 100% rename from sample/helm/http_sample/sender/templates/senderservice.yaml rename to examples/helm/http_sample/sender/templates/senderservice.yaml diff --git a/sample/helm/http_sample/sender/values.yaml b/examples/helm/http_sample/sender/values.yaml similarity index 100% rename from sample/helm/http_sample/sender/values.yaml rename to examples/helm/http_sample/sender/values.yaml diff --git a/internal/tools/helpers/test.go b/internal/tools/helpers/test.go index 9b1a949..ea56857 100644 --- a/internal/tools/helpers/test.go +++ b/internal/tools/helpers/test.go @@ -44,8 +44,8 @@ func PrepareRealFilesForTest(t *testing.T) (string, string, string) { fmt.Print(cwd) - constellationPath := path.Join(cwd, "../sample/constellation/sample_constellation.yaml") - mapsPath := path.Join(cwd, "../sample/constellation/sample_constellation_maps.yaml") + constellationPath := path.Join(cwd, "../examples/constellation/sample_constellation.yaml") + mapsPath := path.Join(cwd, "../examples/constellation/sample_constellation_maps.yaml") return constellationPath, mapsPath, tdir } @@ -69,9 +69,9 @@ func PrepareTwoRealConstellationFilesForTest(t *testing.T) (string, string, stri t.Fatal(err2) } - constellationPathOrg := path.Join(cwd, "../sample/constellation/sample_constellation.yaml") - constellationPathNew := path.Join(cwd, "../sample/constellation/sample_constellation_changed.yaml") - mapsPath := path.Join(cwd, "../sample/constellation/sample_constellation_maps.yaml") + constellationPathOrg := path.Join(cwd, "../examples/constellation/sample_constellation.yaml") + constellationPathNew := path.Join(cwd, "../examples/constellation/sample_constellation_changed.yaml") + mapsPath := path.Join(cwd, "../examples/constellation/sample_constellation_maps.yaml") return constellationPathOrg, constellationPathNew, mapsPath, tdir } diff --git a/makefile b/makefile index 4f18da8..689e7a6 100644 --- a/makefile +++ b/makefile @@ -8,7 +8,7 @@ test-watcher: @echo "Running test watcher" - bash ./.scripts/test_watcher.sh + bash ./scripts/test_watcher.sh lint-all: lint-prepare lint vet @@ -79,7 +79,7 @@ else @exit 111 endif make create-kindcluster - kubectl apply -f /workspace/.scripts/rbac.yaml + kubectl apply -f /workspace/scripts/rbac.yaml install-kind: ifeq (,$(shell which kind)) @@ -100,15 +100,15 @@ build: go build -o abstrakt main.go visualise: build - ./abstrakt visualise -f ./sample/constellation/http_constellation.yaml | dot -Tpng > result.png + ./abstrakt visualise -f ./examples/constellation/http_constellation.yaml | dot -Tpng > result.png diff: build - ./abstrakt diff -o ./sample/constellation/sample_constellation.yaml -n ./sample/constellation/sample_constellation_changed.yaml | dot -Tpng > result.png + ./abstrakt diff -o ./examples/constellation/sample_constellation.yaml -n ./examples/constellation/sample_constellation_changed.yaml | dot -Tpng > result.png run-http-demo: http-demo http-demo-deploy http-demo: build - ./abstrakt compose http-demo -f ./sample/constellation/http_constellation.yaml -m ./sample/constellation/http_constellation_maps.yaml -o ./output/http_sample + ./abstrakt compose http-demo -f ./examples/constellation/http_constellation.yaml -m ./examples/constellation/http_constellation_maps.yaml -o ./output/http_sample http-demo-deploy: helm install wormhole-http-demo ./output/http_sample/http-demo diff --git a/.scripts/rbac.yaml b/scripts/rbac.yaml similarity index 100% rename from .scripts/rbac.yaml rename to scripts/rbac.yaml diff --git a/.scripts/test_watcher.sh b/scripts/test_watcher.sh similarity index 89% rename from .scripts/test_watcher.sh rename to scripts/test_watcher.sh index 8027429..2de4219 100755 --- a/.scripts/test_watcher.sh +++ b/scripts/test_watcher.sh @@ -3,12 +3,10 @@ dogo() { #wire ./internal/serviceLocator go fmt - go generate + go generate gotest -v ./... } - - dogo inotifywait --exclude "[^g].$|[^o]$" -m -r -e close_write ./ | From 8428857c4c0488b57264cb705172a86fd88974f9 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 15 Jan 2020 13:21:15 +1100 Subject: [PATCH 09/85] Moved azure-pipelines.yaml from .azure to build/ci. --- {.azure => build/ci}/azure-pipelines.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {.azure => build/ci}/azure-pipelines.yaml (100%) diff --git a/.azure/azure-pipelines.yaml b/build/ci/azure-pipelines.yaml similarity index 100% rename from .azure/azure-pipelines.yaml rename to build/ci/azure-pipelines.yaml From 099846c7e7850ee9dfef1dbe96827d402ffa62e9 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 15 Jan 2020 13:21:25 +1100 Subject: [PATCH 10/85] Cleanup. --- makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index 689e7a6..ae0dd35 100644 --- a/makefile +++ b/makefile @@ -29,7 +29,7 @@ vet: go vet ./... ################## -# Testing # +# Testing # ################## test-prepare: @@ -90,7 +90,7 @@ else endif ################## -# Run Examples # +# Run Examples # ################## fmt: From 6bfb54d58490326d2bd123de4351e6cde1c44ecb Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 15 Jan 2020 15:18:01 +1100 Subject: [PATCH 11/85] Fixed linting issues. --- cmd/compose_test.go | 2 +- cmd/diff_test.go | 10 ++-- cmd/validate_test.go | 2 +- cmd/version_test.go | 2 +- cmd/visualise_test.go | 48 +------------------ internal/platform/constellation/graph_test.go | 2 +- internal/tools/helpers/test.go | 13 ++--- 7 files changed, 15 insertions(+), 64 deletions(-) diff --git a/cmd/compose_test.go b/cmd/compose_test.go index 6e817a8..bc72a66 100644 --- a/cmd/compose_test.go +++ b/cmd/compose_test.go @@ -42,7 +42,7 @@ func TestComposeCmdVerifyRequiredFlags(t *testing.T) { output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "") if err != nil { - helpers.CheckStringContains(t, err.Error(), expected) + assert.Contains(t, err.Error(), expected) } else { t.Errorf("Expecting error: \n %v\nGot:\n %v\n", expected, output) } diff --git a/cmd/diff_test.go b/cmd/diff_test.go index 288fcf5..02ed656 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -21,7 +21,7 @@ func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { assert.Truef(t, helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, hook.LastEntry().Message), "Expcted output and produced output do not match : expected %s produced %s", testDiffComparisonOutputString, hook.LastEntry().Message) // Did use this initially but wont work with the strongs output from the graphviz library as the sequence of entries in the output can change // while the sequence may change the result is still valid and the same so am usinga local comparison function to get around this problem - // checkStringContains(t, hook.LastEntry().Message, testDiffComparisonOutputString) + // assert.Contains(t, hook.LastEntry().Message, testDiffComparisonOutputString) } } @@ -33,7 +33,7 @@ func TestDffCmdFailYaml(t *testing.T) { output, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "constellationPathOrg", "-n", "constellationPathNew") if err != nil { - helpers.CheckStringContains(t, err.Error(), expected) + assert.Contains(t, err.Error(), expected) } else { t.Errorf("Did not fail and it should have. Expected: %v \nGot: %v", expected, output) } @@ -43,7 +43,7 @@ func TestDffCmdFailYaml(t *testing.T) { output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "constellationPathNew") if err != nil { - helpers.CheckStringContains(t, err.Error(), expected) + assert.Contains(t, err.Error(), expected) } else { t.Errorf("Did not fail and it should have. Expected: %v \nGot: %v", expected, output) } @@ -58,7 +58,7 @@ func TestDiffCmdFailNotYaml(t *testing.T) { output, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "diff.go", "-n", "diff.go") if err != nil { - helpers.CheckStringContains(t, err.Error(), expected) + assert.Contains(t, err.Error(), expected) } else { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } @@ -66,7 +66,7 @@ func TestDiffCmdFailNotYaml(t *testing.T) { output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "diff.go") if err != nil { - helpers.CheckStringContains(t, err.Error(), expected) + assert.Contains(t, err.Error(), expected) } else { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } diff --git a/cmd/validate_test.go b/cmd/validate_test.go index d1677b8..f851e84 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -20,7 +20,7 @@ func TestValidateCommand(t *testing.T) { _, err = helpers.ExecuteCommand(newValidateCmd().cmd, "-f", "does-not-exist") if err != nil { - helpers.CheckStringContains(t, err.Error(), expected) + assert.Contains(t, err.Error(), expected) } else { t.Errorf("Did not received expected error. \nExpected: %v\nGot:\n %v", expected, err.Error()) } diff --git a/cmd/version_test.go b/cmd/version_test.go index bbf18df..3d8a3df 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -29,6 +29,6 @@ func TestVersionCmd(t *testing.T) { if err != nil { t.Error(err) } else { - helpers.CheckStringContains(t, hook.LastEntry().Message, expected) + assert.Contains(t, hook.LastEntry().Message, expected) } } diff --git a/cmd/visualise_test.go b/cmd/visualise_test.go index 95d8ed4..ec0006f 100644 --- a/cmd/visualise_test.go +++ b/cmd/visualise_test.go @@ -32,7 +32,7 @@ func TestVisualiseCmdFailYaml(t *testing.T) { output, err := helpers.ExecuteCommand(newVisualiseCmd().cmd, "-f", "constellationPath") if err != nil { - helpers.CheckStringContains(t, err.Error(), expected) + assert.Contains(t, err.Error(), expected) } else { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } @@ -44,7 +44,7 @@ func TestVisualiseCmdFailNotYaml(t *testing.T) { output, err := helpers.ExecuteCommand(newVisualiseCmd().cmd, "-f", "visualise.go") if err != nil { - helpers.CheckStringContains(t, err.Error(), expected) + assert.Contains(t, err.Error(), expected) } else { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } @@ -98,50 +98,6 @@ func TestParseYaml(t *testing.T) { } } -// Sample DAG file data -const test01DagStr = `Name: "Azure Event Hubs Sample" -Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" -Services: -- Id: "Event Generator" - Type: "EventGenerator" - Properties: {} -- Id: "Azure Event Hub" - Type: "EventHub" - Properties: {} -- Id: "Event Logger" - Type: "EventLogger" - Properties: {} -- Id: "Event Logger" - Type: "EventLogger" - Properties: {} -Relationships: -- Id: "Generator to Event Hubs Link" - Description: "Event Generator to Event Hub connection" - From: "Event Generator" - To: "Azure Event Hub" - Properties: {} -- Id: "Event Hubs to Event Logger Link" - Description: "Event Hubs to Event Logger connection" - From: "Azure Event Hub" - To: "Event Logger" - Properties: {} -- Id: "Event Hubs to Event Logger Link Repeat" - Description: "Event Hubs to Event Logger connection" - From: "Azure Event Hub" - To: "Event Logger" - Properties: {}` - -const test02ConstGraphString = `digraph Azure_Event_Hubs_Sample { - rankdir=LR; - "Event_Generator"->"Azure_Event_Hub"; - "Azure_Event_Hub"->"Event_Logger"; - "Azure_Event_Hub"->"Event_Logger"; - "Azure_Event_Hub"; - "Event_Generator"; - "Event_Logger"; - -} -` const testValidYAMLString = ` Description: "Event Generator to Event Hub connection" From: 9e1bcb3d-ff58-41d4-8779-f71e7b8800f8 diff --git a/internal/platform/constellation/graph_test.go b/internal/platform/constellation/graph_test.go index 126e23b..d39263d 100644 --- a/internal/platform/constellation/graph_test.go +++ b/internal/platform/constellation/graph_test.go @@ -24,7 +24,7 @@ func TestGenerateGraph(t *testing.T) { cmpString := test02ConstGraphString retString, err := retConfig.GenerateGraph(out) - assert.NoErrorf(t, err, "Should not recieve error: %v", err) + assert.NoErrorf(t, err, "Should not receive error: %v", err) assert.True(t, helpers.CompareGraphOutputAsSets(cmpString, retString), "Input graph did not generate expected output graphviz representation\nExpected:\n%v \nGot:\n%v", cmpString, retString) } diff --git a/internal/tools/helpers/test.go b/internal/tools/helpers/test.go index ea56857..545b433 100644 --- a/internal/tools/helpers/test.go +++ b/internal/tools/helpers/test.go @@ -2,7 +2,6 @@ package helpers import ( "bytes" - "fmt" set "github.com/deckarep/golang-set" "github.com/spf13/cobra" "io/ioutil" @@ -12,6 +11,7 @@ import ( "testing" ) +// ExecuteCommand is used to run a cobra command with arguments and return its value and error func ExecuteCommand(root *cobra.Command, args ...string) (output string, err error) { _, output, err = executeCommandC(root, args...) return output, err @@ -25,12 +25,8 @@ func executeCommandC(root *cobra.Command, args ...string) (c *cobra.Command, out return } -func CheckStringContains(t *testing.T, got, expected string) { - if !strings.Contains(got, expected) { - t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got) - } -} - +// PrepareRealFilesForTest creates a new temporary folder with new map and constellation files. +// Returns path to new temp folder func PrepareRealFilesForTest(t *testing.T) (string, string, string) { tdir, err := ioutil.TempDir("./", "output-") if err != nil { @@ -42,8 +38,6 @@ func PrepareRealFilesForTest(t *testing.T) (string, string, string) { t.Fatal(err2) } - fmt.Print(cwd) - constellationPath := path.Join(cwd, "../examples/constellation/sample_constellation.yaml") mapsPath := path.Join(cwd, "../examples/constellation/sample_constellation_maps.yaml") @@ -76,6 +70,7 @@ func PrepareTwoRealConstellationFilesForTest(t *testing.T) (string, string, stri return constellationPathOrg, constellationPathNew, mapsPath, tdir } +// CleanTempTestFiles removes a specific folder and all its contents from disk func CleanTempTestFiles(t *testing.T, temp string) { err := os.RemoveAll(temp) if err != nil { From 16d4fa369d80ea60ef5b3ae92e9cfddc2ff32815 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 12:15:24 +1100 Subject: [PATCH 12/85] Added valid mapper config yaml to testdata. --- internal/platform/mapper/testdata/mapper.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 internal/platform/mapper/testdata/mapper.yaml diff --git a/internal/platform/mapper/testdata/mapper.yaml b/internal/platform/mapper/testdata/mapper.yaml new file mode 100644 index 0000000..819f684 --- /dev/null +++ b/internal/platform/mapper/testdata/mapper.yaml @@ -0,0 +1,15 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "event_hub_sample_event_generator" + Type: "EventGenerator" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_logger" + Type: "EventLogger" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest" + Version: "1.0.0" From 4ede32090f172cf205c2816bf1875f209a1a12cf Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 12:15:39 +1100 Subject: [PATCH 13/85] Added tests for mapper/find.go. --- internal/platform/mapper/find_test.go | 69 +++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 internal/platform/mapper/find_test.go diff --git a/internal/platform/mapper/find_test.go b/internal/platform/mapper/find_test.go new file mode 100644 index 0000000..e745566 --- /dev/null +++ b/internal/platform/mapper/find_test.go @@ -0,0 +1,69 @@ +package mapper_test + +import ( + "github.com/microsoft/abstrakt/internal/platform/mapper" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestFindByName(t *testing.T) { + mapper := &mapper.Config{} + + err := mapper.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + info := mapper.FindByName("event_hub_sample_event_generator") + + assert.NotNil(t, info) + assert.Equal(t, "event_hub_sample_event_generator", info.ChartName) + assert.Equal(t, "EventGenerator", info.Type) + assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "1.0.0", info.Version) + + info = mapper.FindByName("event_hub_sample_event_logger") + + assert.NotNil(t, info) + assert.Equal(t, "event_hub_sample_event_logger", info.ChartName) + assert.Equal(t, "EventLogger", info.Type) + assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "1.0.0", info.Version) + + info = mapper.FindByName("event_hub_sample_event_hub") + + assert.NotNil(t, info) + assert.Equal(t, "event_hub_sample_event_hub", info.ChartName) + assert.Equal(t, "EventHub", info.Type) + assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "1.0.0", info.Version) +} + +func TestFindByType(t *testing.T) { + mapper := &mapper.Config{} + + err := mapper.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + info := mapper.FindByType("EventGenerator") + + assert.NotNil(t, info) + assert.Equal(t, "event_hub_sample_event_generator", info.ChartName) + assert.Equal(t, "EventGenerator", info.Type) + assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "1.0.0", info.Version) + + info = mapper.FindByType("EventLogger") + + assert.NotNil(t, info) + assert.Equal(t, "event_hub_sample_event_logger", info.ChartName) + assert.Equal(t, "EventLogger", info.Type) + assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "1.0.0", info.Version) + + info = mapper.FindByType("EventHub") + + assert.NotNil(t, info) + assert.Equal(t, "event_hub_sample_event_hub", info.ChartName) + assert.Equal(t, "EventHub", info.Type) + assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "1.0.0", info.Version) +} From 642d12062f31df6d2c9aee6f9dca68fc3c5a8798 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 12:16:03 +1100 Subject: [PATCH 14/85] Added additional test to mapper_test to test loading of mapper file. --- internal/platform/mapper/mapper_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/platform/mapper/mapper_test.go b/internal/platform/mapper/mapper_test.go index 6c17bf4..8be78c8 100644 --- a/internal/platform/mapper/mapper_test.go +++ b/internal/platform/mapper/mapper_test.go @@ -38,6 +38,15 @@ func TestMapFromString(t *testing.T) { } } +func TestMapLoadFile(t *testing.T) { + mapper := &mapper.Config{} + + err := mapper.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + assert.Truef(t, reflect.DeepEqual(&buildMap01, mapper), "Expected: %v\nGot: %v", &buildMap01, mapper) +} + const configMapTest01String = ` Name: "Basic Azure Event Hubs maps" Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" From 08e712b422d16073da5f93b6bcde47ff0fc50f1f Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 12:17:56 +1100 Subject: [PATCH 15/85] Added validation tags to mapper struct. --- internal/platform/mapper/mapper.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/platform/mapper/mapper.go b/internal/platform/mapper/mapper.go index b3a6670..f8b114e 100644 --- a/internal/platform/mapper/mapper.go +++ b/internal/platform/mapper/mapper.go @@ -19,10 +19,10 @@ import ( // Info -- info about an individual component type Info struct { - ChartName string `yaml:"ChartName"` - Type string `yaml:"Type"` - Location string `yaml:"Location"` - Version string `yaml:"Version"` + ChartName string `yaml:"ChartName" validate:"empty=false"` + Type string `yaml:"Type" validate:"empty=false"` + Location string `yaml:"Location" validate:"empty=false"` + Version string `yaml:"Version" validate:"empty=false"` } // Config -- data from the entire build map. From b8a1268f5693b03ba1149cde8f535af157a23987 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 15:01:31 +1100 Subject: [PATCH 16/85] Moved Find to under helper package. --- internal/platform/constellation/validate.go | 24 +++++++-------------- internal/tools/helpers/find.go | 12 +++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 internal/tools/helpers/find.go diff --git a/internal/platform/constellation/validate.go b/internal/platform/constellation/validate.go index e8d1853..045128f 100644 --- a/internal/platform/constellation/validate.go +++ b/internal/platform/constellation/validate.go @@ -1,13 +1,16 @@ package constellation -import "gopkg.in/dealancer/validate.v2" +import ( + "github.com/microsoft/abstrakt/internal/tools/helpers" + "gopkg.in/dealancer/validate.v2" +) // CheckDuplicates checks for duplicate Relationship and Service IDs in a constellation file. func (m *Config) CheckDuplicates() (duplicates []string) { IDs := []string{string(m.ID)} for _, i := range m.Services { - _, exists := find(IDs, i.ID) + _, exists := helpers.Find(IDs, i.ID) if exists { duplicates = append(duplicates, i.ID) } else { @@ -16,7 +19,7 @@ func (m *Config) CheckDuplicates() (duplicates []string) { } for _, i := range m.Relationships { - _, exists := find(IDs, i.ID) + _, exists := helpers.Find(IDs, i.ID) if exists { duplicates = append(duplicates, i.ID) } else { @@ -37,12 +40,12 @@ func (m *Config) CheckServiceExists() (missing map[string][]string) { } for _, i := range m.Relationships { - _, exists := find(IDs, i.To) + _, exists := helpers.Find(IDs, i.To) if !exists { missing[i.ID] = append(missing[i.ID], i.To) } - _, exists = find(IDs, i.From) + _, exists = helpers.Find(IDs, i.From) if !exists { missing[i.ID] = append(missing[i.ID], i.From) } @@ -55,14 +58,3 @@ func (m *Config) CheckServiceExists() (missing map[string][]string) { func (m *Config) ValidateModel() error { return validate.Validate(m) } - -// find takes a slice and looks for an element in it. If found it will -// return it's key, otherwise it will return -1 and a bool of false. -func find(slice []string, val string) (int, bool) { - for i, item := range slice { - if item == val { - return i, true - } - } - return -1, false -} diff --git a/internal/tools/helpers/find.go b/internal/tools/helpers/find.go new file mode 100644 index 0000000..669eddb --- /dev/null +++ b/internal/tools/helpers/find.go @@ -0,0 +1,12 @@ +package helpers + +// Find takes a slice and looks for an element in it. If found it will +// return it's key, otherwise it will return -1 and a bool of false. +func Find(slice []string, val string) (int, bool) { + for i, item := range slice { + if item == val { + return i, true + } + } + return -1, false +} From baee179923e8527083c783cbd8a705593eba27e2 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 15:02:02 +1100 Subject: [PATCH 17/85] Updated validate_test. --- .../platform/constellation/validate_test.go | 58 ++++++++++++------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/internal/platform/constellation/validate_test.go b/internal/platform/constellation/validate_test.go index 7a26b68..a62607a 100644 --- a/internal/platform/constellation/validate_test.go +++ b/internal/platform/constellation/validate_test.go @@ -8,34 +8,39 @@ import ( func TestForDuplicatIDsInServices(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/valid.yaml") + + err := testData.LoadFile("testdata/valid.yaml") + assert.NoError(t, err) duplicates := testData.CheckDuplicates() - assert.Nil(t, duplicates, "No duplicates should be found.") } func TestServicesExists(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/valid.yaml") + + err := testData.LoadFile("testdata/valid.yaml") + assert.NoError(t, err) missing := testData.CheckServiceExists() - assert.Empty(t, missing, "No missing services should be found.") } func TestSchemaChecks(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/valid.yaml") - err := testData.ValidateModel() + err := testData.LoadFile("testdata/valid.yaml") + assert.NoError(t, err) + err = testData.ValidateModel() assert.NoError(t, err, "Model validation should not return errors") } func TestForDuplicatIDsInServicesFail(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/duplicate/servIds.yaml") + + err := testData.LoadFile("testdata/duplicate/servIds.yaml") + assert.NoError(t, err) duplicates := testData.CheckDuplicates() @@ -45,7 +50,9 @@ func TestForDuplicatIDsInServicesFail(t *testing.T) { func TestForDuplicatIDsInRelationshipsFail(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/duplicate/relIds.yaml") + + err := testData.LoadFile("testdata/duplicate/relIds.yaml") + assert.NoError(t, err) duplicates := testData.CheckDuplicates() @@ -55,7 +62,9 @@ func TestForDuplicatIDsInRelationshipsFail(t *testing.T) { func TestForDuplicatIDsFail(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/duplicate/servRelIds.yaml") + + err := testData.LoadFile("testdata/duplicate/servRelIds.yaml") + assert.NoError(t, err) duplicates := testData.CheckDuplicates() @@ -66,10 +75,11 @@ func TestForDuplicatIDsFail(t *testing.T) { func TestServicesExistsFail(t *testing.T) { expected := "Azure Event Hub" testData := new(constellation.Config) - _ = testData.LoadFile("testdata/missing/relServRefId.yaml") + + err := testData.LoadFile("testdata/missing/relServRefId.yaml") + assert.NoError(t, err) missing := testData.CheckServiceExists() - foundID := missing["Event Hubs to Event Logger Link"][0] assert.Equal(t, 1, len(missing), "There should be only 1 missing services found") @@ -78,45 +88,49 @@ func TestServicesExistsFail(t *testing.T) { func TestSchemaMissingDagName(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/missingName.yaml") - - err := testData.ValidateModel() + err := testData.LoadFile("testdata/missingName.yaml") + assert.NoError(t, err) + err = testData.ValidateModel() assert.Error(t, err, "Model validation should be invalid") } func TestSchemaMissingDagID(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/missing/id.yaml") - err := testData.ValidateModel() + err := testData.LoadFile("testdata/missing/id.yaml") + assert.NoError(t, err) + err = testData.ValidateModel() assert.Error(t, err, "Model validation should be invalid") } func TestSchemaMissingService(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/missing/serv.yaml") - err := testData.ValidateModel() + err := testData.LoadFile("testdata/missing/serv.yaml") + assert.NoError(t, err) + err = testData.ValidateModel() assert.Error(t, err, "Model validation should be invalid") } func TestSchemaMissingServiceID(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/missing/servId.yaml") - err := testData.ValidateModel() + err := testData.LoadFile("testdata/missing/servId.yaml") + assert.NoError(t, err) + err = testData.ValidateModel() assert.Error(t, err, "Model validation should be invalid") } func TestSchemaMissingRelationshipID(t *testing.T) { testData := new(constellation.Config) - _ = testData.LoadFile("testdata/missing/relId.yaml") - err := testData.ValidateModel() + err := testData.LoadFile("testdata/missing/relId.yaml") + assert.NoError(t, err) + err = testData.ValidateModel() assert.Error(t, err, "Model validation should be invalid") } From 5b3d705e91e15182003ea41b36eecc70d71c8944 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 15:03:23 +1100 Subject: [PATCH 18/85] Use `new()` to create new instance of struct. --- internal/platform/mapper/find_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/platform/mapper/find_test.go b/internal/platform/mapper/find_test.go index e745566..2bce287 100644 --- a/internal/platform/mapper/find_test.go +++ b/internal/platform/mapper/find_test.go @@ -7,7 +7,7 @@ import ( ) func TestFindByName(t *testing.T) { - mapper := &mapper.Config{} + mapper := new(mapper.Config) err := mapper.LoadFile("testdata/mapper.yaml") assert.NoError(t, err) @@ -38,7 +38,7 @@ func TestFindByName(t *testing.T) { } func TestFindByType(t *testing.T) { - mapper := &mapper.Config{} + mapper := new(mapper.Config) err := mapper.LoadFile("testdata/mapper.yaml") assert.NoError(t, err) From 3c74c7c1da5552a6d67876c21b02502cf3b9edd9 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 15:03:48 +1100 Subject: [PATCH 19/85] Add mapping yaml test data. --- .../mapper/testdata/invalid/invalidType.yaml | 15 +++++++++++++++ .../testdata/invalid/missingChartNameValue.yaml | 15 +++++++++++++++ .../testdata/invalid/missingVersionProperty.yaml | 14 ++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 internal/platform/mapper/testdata/invalid/invalidType.yaml create mode 100644 internal/platform/mapper/testdata/invalid/missingChartNameValue.yaml create mode 100644 internal/platform/mapper/testdata/invalid/missingVersionProperty.yaml diff --git a/internal/platform/mapper/testdata/invalid/invalidType.yaml b/internal/platform/mapper/testdata/invalid/invalidType.yaml new file mode 100644 index 0000000..ed4d771 --- /dev/null +++ b/internal/platform/mapper/testdata/invalid/invalidType.yaml @@ -0,0 +1,15 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "event_hub_sample_event_generator" + Type: "EventGenerator" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_logger" + Type: "EventLogger" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest" + Version: 1 diff --git a/internal/platform/mapper/testdata/invalid/missingChartNameValue.yaml b/internal/platform/mapper/testdata/invalid/missingChartNameValue.yaml new file mode 100644 index 0000000..13fb487 --- /dev/null +++ b/internal/platform/mapper/testdata/invalid/missingChartNameValue.yaml @@ -0,0 +1,15 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "" + Type: "EventGenerator" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_logger" + Type: "EventLogger" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest" + Version: "1.0.0" diff --git a/internal/platform/mapper/testdata/invalid/missingVersionProperty.yaml b/internal/platform/mapper/testdata/invalid/missingVersionProperty.yaml new file mode 100644 index 0000000..3bf607a --- /dev/null +++ b/internal/platform/mapper/testdata/invalid/missingVersionProperty.yaml @@ -0,0 +1,14 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "event_hub_sample_event_generator" + Type: "EventGenerator" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_logger" + Type: "EventLogger" + Location: "../../helm/basictest" +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest" + Version: "1.0.0" From 334b857c8d3a25fc013e8be03b38bd4c02661893 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 15:04:25 +1100 Subject: [PATCH 20/85] Added format type for mapper validation. --- internal/platform/mapper/mapper.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/platform/mapper/mapper.go b/internal/platform/mapper/mapper.go index f8b114e..14166cb 100644 --- a/internal/platform/mapper/mapper.go +++ b/internal/platform/mapper/mapper.go @@ -19,10 +19,10 @@ import ( // Info -- info about an individual component type Info struct { - ChartName string `yaml:"ChartName" validate:"empty=false"` - Type string `yaml:"Type" validate:"empty=false"` - Location string `yaml:"Location" validate:"empty=false"` - Version string `yaml:"Version" validate:"empty=false"` + ChartName string `yaml:"ChartName" validate:"empty=false & format=string"` + Type string `yaml:"Type" validate:"empty=false & format=string"` + Location string `yaml:"Location" validate:"empty=false & format=string"` + Version string `yaml:"Version" validate:"empty=false & format=string"` } // Config -- data from the entire build map. From ca4eb159e1e2272010956ad46d4b13c2db16cd6d Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 16 Jan 2020 15:04:49 +1100 Subject: [PATCH 21/85] Adding mapper validation. --- internal/platform/mapper/validate.go | 27 ++++++++++++++++++ internal/platform/mapper/validate_test.go | 34 +++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 internal/platform/mapper/validate.go create mode 100644 internal/platform/mapper/validate_test.go diff --git a/internal/platform/mapper/validate.go b/internal/platform/mapper/validate.go new file mode 100644 index 0000000..09d6112 --- /dev/null +++ b/internal/platform/mapper/validate.go @@ -0,0 +1,27 @@ +package mapper + +import ( + "github.com/microsoft/abstrakt/internal/tools/helpers" + "gopkg.in/dealancer/validate.v2" +) + +// CheckDuplicates checks for duplicate chart names in a mapper file. +func (m *Config) CheckDuplicates() (duplicates []string) { + chartNames := []string{} + + for _, i := range m.Maps { + _, exists := helpers.Find(chartNames, i.ChartName) + if exists { + duplicates = append(duplicates, i.ChartName) + } else { + chartNames = append(chartNames, i.ChartName) + } + } + + return +} + +// ValidateModel checks if mapper has all required felids +func (m *Config) ValidateModel() error { + return validate.Validate(m) +} diff --git a/internal/platform/mapper/validate_test.go b/internal/platform/mapper/validate_test.go new file mode 100644 index 0000000..da87d35 --- /dev/null +++ b/internal/platform/mapper/validate_test.go @@ -0,0 +1,34 @@ +package mapper_test + +import ( + "github.com/microsoft/abstrakt/internal/platform/mapper" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestSchemaFailMissingChartNameValue(t *testing.T) { + testData := new(mapper.Config) + err := testData.LoadFile("testdata/invalid/missingChartNameValue.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should return error") +} + +func TestSchemaFailMissingVersionProperty(t *testing.T) { + testData := new(mapper.Config) + err := testData.LoadFile("testdata/invalid/missingVersionProperty.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should return error") +} + +func TestSchemaFailInvalidType(t *testing.T) { + testData := new(mapper.Config) + err := testData.LoadFile("testdata/invalid/invalidType.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should return error") +} From 275ccbdec5461bfc1c4e53959728102c3fa7354a Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 17 Jan 2020 10:57:09 +1100 Subject: [PATCH 22/85] Updated validation tests. --- internal/platform/mapper/mapper.go | 10 +++++----- .../mapper/testdata/invalid/invalidType.yaml | 15 --------------- .../mapper/testdata/invalid/missingMap.yaml | 3 +++ internal/platform/mapper/validate_test.go | 13 +++++++++++-- 4 files changed, 19 insertions(+), 22 deletions(-) delete mode 100644 internal/platform/mapper/testdata/invalid/invalidType.yaml create mode 100644 internal/platform/mapper/testdata/invalid/missingMap.yaml diff --git a/internal/platform/mapper/mapper.go b/internal/platform/mapper/mapper.go index 14166cb..75aadb6 100644 --- a/internal/platform/mapper/mapper.go +++ b/internal/platform/mapper/mapper.go @@ -19,17 +19,17 @@ import ( // Info -- info about an individual component type Info struct { - ChartName string `yaml:"ChartName" validate:"empty=false & format=string"` - Type string `yaml:"Type" validate:"empty=false & format=string"` - Location string `yaml:"Location" validate:"empty=false & format=string"` - Version string `yaml:"Version" validate:"empty=false & format=string"` + ChartName string `yaml:"ChartName" validate:"empty=false"` + Type string `yaml:"Type" validate:"empty=false"` + Location string `yaml:"Location" validate:"empty=false"` + Version string `yaml:"Version" validate:"empty=false"` } // Config -- data from the entire build map. type Config struct { Name string `yaml:"Name"` ID guid.GUID `yaml:"Id"` - Maps []Info `yaml:"Maps"` + Maps []Info `yaml:"Maps" validate:"empty=false"` } // LoadFile -- New Map info instance from the named file. diff --git a/internal/platform/mapper/testdata/invalid/invalidType.yaml b/internal/platform/mapper/testdata/invalid/invalidType.yaml deleted file mode 100644 index ed4d771..0000000 --- a/internal/platform/mapper/testdata/invalid/invalidType.yaml +++ /dev/null @@ -1,15 +0,0 @@ -Name: "Basic Azure Event Hubs maps" -Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" -Maps: -- ChartName: "event_hub_sample_event_generator" - Type: "EventGenerator" - Location: "../../helm/basictest" - Version: "1.0.0" -- ChartName: "event_hub_sample_event_logger" - Type: "EventLogger" - Location: "../../helm/basictest" - Version: "1.0.0" -- ChartName: "event_hub_sample_event_hub" - Type: "EventHub" - Location: "../../helm/basictest" - Version: 1 diff --git a/internal/platform/mapper/testdata/invalid/missingMap.yaml b/internal/platform/mapper/testdata/invalid/missingMap.yaml new file mode 100644 index 0000000..2befe8d --- /dev/null +++ b/internal/platform/mapper/testdata/invalid/missingMap.yaml @@ -0,0 +1,3 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: [] diff --git a/internal/platform/mapper/validate_test.go b/internal/platform/mapper/validate_test.go index da87d35..3b2ccf3 100644 --- a/internal/platform/mapper/validate_test.go +++ b/internal/platform/mapper/validate_test.go @@ -7,28 +7,37 @@ import ( ) func TestSchemaFailMissingChartNameValue(t *testing.T) { + expected := "Validation error in field \"ChartName\" of type \"string\" using validator \"empty=false\"" testData := new(mapper.Config) + err := testData.LoadFile("testdata/invalid/missingChartNameValue.yaml") assert.NoError(t, err) err = testData.ValidateModel() assert.Error(t, err, "Model validation should return error") + assert.EqualError(t, err, expected, "Model validation should return error") } func TestSchemaFailMissingVersionProperty(t *testing.T) { + expected := "Validation error in field \"Version\" of type \"string\" using validator \"empty=false\"" testData := new(mapper.Config) + err := testData.LoadFile("testdata/invalid/missingVersionProperty.yaml") assert.NoError(t, err) err = testData.ValidateModel() assert.Error(t, err, "Model validation should return error") + assert.EqualError(t, err, expected, "Model validation should return error") } -func TestSchemaFailInvalidType(t *testing.T) { +func TestSchemaFailMissingMap(t *testing.T) { + expected := "Validation error in field \"Maps\" of type \"[]mapper.Info\" using validator \"empty=false\"" testData := new(mapper.Config) - err := testData.LoadFile("testdata/invalid/invalidType.yaml") + + err := testData.LoadFile("testdata/invalid/missingMap.yaml") assert.NoError(t, err) err = testData.ValidateModel() assert.Error(t, err, "Model validation should return error") + assert.EqualError(t, err, expected, "Model validation should return error") } From 940b0588a9bfe9816cb8c0b37cc00c32656563eb Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 17 Jan 2020 10:57:24 +1100 Subject: [PATCH 23/85] Added mapper validator to cmd. --- cmd/validate.go | 80 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 9 deletions(-) diff --git a/cmd/validate.go b/cmd/validate.go index 8f2b7b3..c115cbd 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -3,12 +3,14 @@ package cmd import ( "fmt" "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/platform/mapper" "github.com/microsoft/abstrakt/internal/tools/logger" "github.com/spf13/cobra" ) type validateCmd struct { constellationFilePath string + mapperFilePath string *baseCmd } @@ -25,17 +27,34 @@ Example: abstrakt validate -f [constellationFilePath]`, SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) (err error) { - d := new(constellation.Config) - err = d.LoadFile(cc.constellationFilePath) - - if err != nil { - return + if len(cc.constellationFilePath) == 0 && len(cc.mapperFilePath) == 0 { + return fmt.Errorf("No arguments passed") } - err = validateDag(d) + fail := false - if err == nil { - logger.Info("Constellation is valid.") + if len(cc.mapperFilePath) > 0 { + err = loadAndTestMapper(cc.mapperFilePath) + if err != nil { + logger.Errorf("Mapper: %v", err) + fail = true + } else { + logger.Info("Mapper: valid") + } + } + + if len(cc.constellationFilePath) > 0 { + err = loadAndTestDag(cc.constellationFilePath) + if err != nil { + logger.Errorf("Constellation: %v", err) + fail = true + } else { + logger.Info("Constellation: valid") + } + } + + if fail { + err = fmt.Errorf("Invalid configuration(s)") } return @@ -43,11 +62,22 @@ Example: abstrakt validate -f [constellationFilePath]`, }) cc.cmd.Flags().StringVarP(&cc.constellationFilePath, "constellationFilePath", "f", "", "constellation file path") - _ = cc.cmd.MarkFlagRequired("constellationFilePath") + cc.cmd.Flags().StringVarP(&cc.mapperFilePath, "mapperFilePath", "m", "", "mapper file path") return cc } +func loadAndTestDag(path string) (err error) { + d := new(constellation.Config) + err = d.LoadFile(path) + + if err != nil { + return + } + + return validateDag(d) +} + // validateDag takes a constellation dag and returns any errors. func validateDag(dag *constellation.Config) (err error) { err = dag.ValidateModel() @@ -80,3 +110,35 @@ func validateDag(dag *constellation.Config) (err error) { return } + +func loadAndTestMapper(path string) (err error) { + m := new(mapper.Config) + err = m.LoadFile(path) + + if err != nil { + return + } + + return validateMapper(m) +} + +// validateMapper takes a constellation mapper and returns any errors. +func validateMapper(mapper *mapper.Config) (err error) { + err = mapper.ValidateModel() + + if err != nil { + return + } + + duplicates := mapper.CheckDuplicates() + + if duplicates != nil { + logger.Error("Duplicate IDs found:") + for _, i := range duplicates { + logger.Errorf("'%v'", i) + } + err = error(fmt.Errorf("Constellation is invalid")) + } + + return +} From 9f5b7582203161b9e097a2b9514ae164d396ab2f Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 17 Jan 2020 13:20:58 +1100 Subject: [PATCH 24/85] Fixed constellation validation unit test. --- internal/platform/constellation/validate_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/platform/constellation/validate_test.go b/internal/platform/constellation/validate_test.go index a62607a..d66e3a9 100644 --- a/internal/platform/constellation/validate_test.go +++ b/internal/platform/constellation/validate_test.go @@ -88,7 +88,7 @@ func TestServicesExistsFail(t *testing.T) { func TestSchemaMissingDagName(t *testing.T) { testData := new(constellation.Config) - err := testData.LoadFile("testdata/missingName.yaml") + err := testData.LoadFile("testdata/missing/name.yaml") assert.NoError(t, err) err = testData.ValidateModel() From 895cb0ed2fb24c3bcdd43a03b1b55b8a41a095e1 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 20 Jan 2020 12:56:08 +1100 Subject: [PATCH 25/85] Updated validator tests. --- .../mapper/testdata/duplicate/chartNames.yaml | 15 +++ .../locations.yaml} | 1 + .../mapper/testdata/duplicate/types.yaml | 15 +++ internal/platform/mapper/testdata/mapper.yaml | 4 +- .../chartNameValue.yaml} | 4 +- .../missingMap.yaml => missing/map.yaml} | 0 .../testdata/missing/versionProperty.yaml | 14 +++ internal/platform/mapper/validate.go | 36 +++++++- internal/platform/mapper/validate_test.go | 91 ++++++++++++++++++- 9 files changed, 171 insertions(+), 9 deletions(-) create mode 100644 internal/platform/mapper/testdata/duplicate/chartNames.yaml rename internal/platform/mapper/testdata/{invalid/missingVersionProperty.yaml => duplicate/locations.yaml} (95%) create mode 100644 internal/platform/mapper/testdata/duplicate/types.yaml rename internal/platform/mapper/testdata/{invalid/missingChartNameValue.yaml => missing/chartNameValue.yaml} (82%) rename internal/platform/mapper/testdata/{invalid/missingMap.yaml => missing/map.yaml} (100%) create mode 100644 internal/platform/mapper/testdata/missing/versionProperty.yaml diff --git a/internal/platform/mapper/testdata/duplicate/chartNames.yaml b/internal/platform/mapper/testdata/duplicate/chartNames.yaml new file mode 100644 index 0000000..c659b65 --- /dev/null +++ b/internal/platform/mapper/testdata/duplicate/chartNames.yaml @@ -0,0 +1,15 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "event_hub_sample_event_generator" + Type: "EventGenerator" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_generator" + Type: "EventLogger" + Location: "../../helm/basictest2" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_generator" + Type: "EventHub" + Location: "../../helm/basictest3" + Version: "1.0.0" diff --git a/internal/platform/mapper/testdata/invalid/missingVersionProperty.yaml b/internal/platform/mapper/testdata/duplicate/locations.yaml similarity index 95% rename from internal/platform/mapper/testdata/invalid/missingVersionProperty.yaml rename to internal/platform/mapper/testdata/duplicate/locations.yaml index 3bf607a..819f684 100644 --- a/internal/platform/mapper/testdata/invalid/missingVersionProperty.yaml +++ b/internal/platform/mapper/testdata/duplicate/locations.yaml @@ -8,6 +8,7 @@ Maps: - ChartName: "event_hub_sample_event_logger" Type: "EventLogger" Location: "../../helm/basictest" + Version: "1.0.0" - ChartName: "event_hub_sample_event_hub" Type: "EventHub" Location: "../../helm/basictest" diff --git a/internal/platform/mapper/testdata/duplicate/types.yaml b/internal/platform/mapper/testdata/duplicate/types.yaml new file mode 100644 index 0000000..0b837b0 --- /dev/null +++ b/internal/platform/mapper/testdata/duplicate/types.yaml @@ -0,0 +1,15 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "event_hub_sample_event_generator" + Type: "EventHub" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_logger" + Type: "EventHub" + Location: "../../helm/basictest2" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest3" + Version: "1.0.0" diff --git a/internal/platform/mapper/testdata/mapper.yaml b/internal/platform/mapper/testdata/mapper.yaml index 819f684..033bb78 100644 --- a/internal/platform/mapper/testdata/mapper.yaml +++ b/internal/platform/mapper/testdata/mapper.yaml @@ -7,9 +7,9 @@ Maps: Version: "1.0.0" - ChartName: "event_hub_sample_event_logger" Type: "EventLogger" - Location: "../../helm/basictest" + Location: "../../helm/basictest2" Version: "1.0.0" - ChartName: "event_hub_sample_event_hub" Type: "EventHub" - Location: "../../helm/basictest" + Location: "../../helm/basictest3" Version: "1.0.0" diff --git a/internal/platform/mapper/testdata/invalid/missingChartNameValue.yaml b/internal/platform/mapper/testdata/missing/chartNameValue.yaml similarity index 82% rename from internal/platform/mapper/testdata/invalid/missingChartNameValue.yaml rename to internal/platform/mapper/testdata/missing/chartNameValue.yaml index 13fb487..7fb17f7 100644 --- a/internal/platform/mapper/testdata/invalid/missingChartNameValue.yaml +++ b/internal/platform/mapper/testdata/missing/chartNameValue.yaml @@ -7,9 +7,9 @@ Maps: Version: "1.0.0" - ChartName: "event_hub_sample_event_logger" Type: "EventLogger" - Location: "../../helm/basictest" + Location: "../../helm/basictest2" Version: "1.0.0" - ChartName: "event_hub_sample_event_hub" Type: "EventHub" - Location: "../../helm/basictest" + Location: "../../helm/basictest3" Version: "1.0.0" diff --git a/internal/platform/mapper/testdata/invalid/missingMap.yaml b/internal/platform/mapper/testdata/missing/map.yaml similarity index 100% rename from internal/platform/mapper/testdata/invalid/missingMap.yaml rename to internal/platform/mapper/testdata/missing/map.yaml diff --git a/internal/platform/mapper/testdata/missing/versionProperty.yaml b/internal/platform/mapper/testdata/missing/versionProperty.yaml new file mode 100644 index 0000000..5a9b0e1 --- /dev/null +++ b/internal/platform/mapper/testdata/missing/versionProperty.yaml @@ -0,0 +1,14 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "event_hub_sample_event_generator" + Type: "EventGenerator" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_logger" + Type: "EventLogger" + Location: "../../helm/basictest2" +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest3" + Version: "1.0.0" diff --git a/internal/platform/mapper/validate.go b/internal/platform/mapper/validate.go index 09d6112..42f377f 100644 --- a/internal/platform/mapper/validate.go +++ b/internal/platform/mapper/validate.go @@ -5,8 +5,8 @@ import ( "gopkg.in/dealancer/validate.v2" ) -// CheckDuplicates checks for duplicate chart names in a mapper file. -func (m *Config) CheckDuplicates() (duplicates []string) { +// DuplicateChartName checks for duplicate chart names in a mapper file. +func (m *Config) DuplicateChartName() (duplicates []string) { chartNames := []string{} for _, i := range m.Maps { @@ -21,6 +21,38 @@ func (m *Config) CheckDuplicates() (duplicates []string) { return } +// DuplicateType checks for duplicate types in a mapper file. +func (m *Config) DuplicateType() (duplicates []string) { + types := []string{} + + for _, i := range m.Maps { + _, exists := helpers.Find(types, i.Type) + if exists { + duplicates = append(duplicates, i.Type) + } else { + types = append(types, i.Type) + } + } + + return +} + +// DuplicateLocation checks for duplicate location in a mapper file. +func (m *Config) DuplicateLocation() (duplicates []string) { + location := []string{} + + for _, i := range m.Maps { + _, exists := helpers.Find(location, i.Location) + if exists { + duplicates = append(duplicates, i.Location) + } else { + location = append(location, i.Location) + } + } + + return +} + // ValidateModel checks if mapper has all required felids func (m *Config) ValidateModel() error { return validate.Validate(m) diff --git a/internal/platform/mapper/validate_test.go b/internal/platform/mapper/validate_test.go index 3b2ccf3..19dbbd0 100644 --- a/internal/platform/mapper/validate_test.go +++ b/internal/platform/mapper/validate_test.go @@ -6,11 +6,51 @@ import ( "testing" ) +func TestSchemaPass(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.NoError(t, err) +} + +func TestDuplicateChartNamePass(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + duplicate := testData.DuplicateChartName() + assert.Nil(t, duplicate) +} + +func TestDuplicateTypesPass(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + duplicate := testData.DuplicateType() + assert.Nil(t, duplicate) +} + +func TestDuplicateLocationsPass(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + duplicate := testData.DuplicateLocation() + assert.Nil(t, duplicate) +} + func TestSchemaFailMissingChartNameValue(t *testing.T) { expected := "Validation error in field \"ChartName\" of type \"string\" using validator \"empty=false\"" testData := new(mapper.Config) - err := testData.LoadFile("testdata/invalid/missingChartNameValue.yaml") + err := testData.LoadFile("testdata/missing/chartNameValue.yaml") assert.NoError(t, err) err = testData.ValidateModel() @@ -22,7 +62,7 @@ func TestSchemaFailMissingVersionProperty(t *testing.T) { expected := "Validation error in field \"Version\" of type \"string\" using validator \"empty=false\"" testData := new(mapper.Config) - err := testData.LoadFile("testdata/invalid/missingVersionProperty.yaml") + err := testData.LoadFile("testdata/missing/versionProperty.yaml") assert.NoError(t, err) err = testData.ValidateModel() @@ -34,10 +74,55 @@ func TestSchemaFailMissingMap(t *testing.T) { expected := "Validation error in field \"Maps\" of type \"[]mapper.Info\" using validator \"empty=false\"" testData := new(mapper.Config) - err := testData.LoadFile("testdata/invalid/missingMap.yaml") + err := testData.LoadFile("testdata/missing/map.yaml") assert.NoError(t, err) err = testData.ValidateModel() assert.Error(t, err, "Model validation should return error") assert.EqualError(t, err, expected, "Model validation should return error") } + +func TestDuplicateChartName(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/duplicate/chartNames.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.NoError(t, err, "Model validation should not return error") + + duplicate := testData.DuplicateChartName() + assert.NotNil(t, duplicate) + assert.Equal(t, 2, len(duplicate)) + assert.Equal(t, "event_hub_sample_event_generator", duplicate[0]) +} + +func TestDuplicateTypes(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/duplicate/types.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.NoError(t, err, "Model validation should not return error") + + duplicate := testData.DuplicateType() + assert.NotNil(t, duplicate) + assert.Equal(t, 2, len(duplicate)) + assert.Equal(t, "EventHub", duplicate[0]) +} + +func TestDuplicateLocation(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/duplicate/locations.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.NoError(t, err, "Model validation should not return error") + + duplicate := testData.DuplicateLocation() + assert.NotNil(t, duplicate) + assert.Equal(t, 2, len(duplicate)) + assert.Equal(t, "../../helm/basictest", duplicate[0]) +} From 35764bc7ae25877af224f12fc1a4f150cf68a832 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 20 Jan 2020 12:56:32 +1100 Subject: [PATCH 26/85] Added IsEmpty to mapper and constellation. --- internal/platform/constellation/constellation.go | 6 ++++++ internal/platform/mapper/mapper.go | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/internal/platform/constellation/constellation.go b/internal/platform/constellation/constellation.go index 52a08e3..1771e04 100644 --- a/internal/platform/constellation/constellation.go +++ b/internal/platform/constellation/constellation.go @@ -17,6 +17,7 @@ import ( "github.com/microsoft/abstrakt/internal/tools/guid" yamlParser "gopkg.in/yaml.v2" "io/ioutil" + "reflect" ) // Note: the yaml mappings are necessary (despite the 1-1 name correspondence). @@ -69,3 +70,8 @@ func (m *Config) LoadFile(fileName string) (err error) { func (m *Config) LoadString(yamlString string) (err error) { return yamlParser.Unmarshal([]byte(yamlString), m) } + +//IsEmpty checks if config is empty. +func (m *Config) IsEmpty() bool { + return reflect.DeepEqual(Config{}, *m) +} diff --git a/internal/platform/mapper/mapper.go b/internal/platform/mapper/mapper.go index 75aadb6..6b06455 100644 --- a/internal/platform/mapper/mapper.go +++ b/internal/platform/mapper/mapper.go @@ -10,6 +10,7 @@ import ( "github.com/microsoft/abstrakt/internal/tools/guid" yamlParser "gopkg.in/yaml.v2" "io/ioutil" + "reflect" ) // Note: the yaml mapping attributes are necessary (despite the nearly @@ -45,3 +46,8 @@ func (m *Config) LoadFile(fileName string) (err error) { func (m *Config) LoadString(yamlString string) error { return yamlParser.Unmarshal([]byte(yamlString), m) } + +//IsEmpty checks if config is empty. +func (m *Config) IsEmpty() bool { + return reflect.DeepEqual(Config{}, *m) +} From fca1d52659d13b3dbd6c834f993fc8926910d97a Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 20 Jan 2020 13:06:20 +1100 Subject: [PATCH 27/85] Added additional tests for IsEmpty. --- .../constellation/constellation_test.go | 19 ++++++++++ internal/platform/mapper/mapper_test.go | 38 +++++++++---------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/internal/platform/constellation/constellation_test.go b/internal/platform/constellation/constellation_test.go index 2551424..ae5530e 100644 --- a/internal/platform/constellation/constellation_test.go +++ b/internal/platform/constellation/constellation_test.go @@ -43,6 +43,25 @@ func TestNewDagConfigFromString(t *testing.T) { } } +func TestMapLoadFile(t *testing.T) { + dag := &constellation.Config{} + + err := dag.LoadFile("testdata/valid.yaml") + assert.NoError(t, err) + + assert.Truef(t, reflect.DeepEqual(&test01WantDag, dag), "Expected: %v\nGot: %v", &test01WantDag, dag) +} + +func TestIsEmptyTrue(t *testing.T) { + dag := &constellation.Config{} + assert.True(t, dag.IsEmpty()) +} + +func TestIsEmptyFalse(t *testing.T) { + dag := test01WantDag + assert.False(t, dag.IsEmpty()) +} + var test01WantDag constellation.Config = constellation.Config{ Name: "Azure Event Hubs Sample", ID: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), diff --git a/internal/platform/mapper/mapper_test.go b/internal/platform/mapper/mapper_test.go index 8be78c8..3cc0d07 100644 --- a/internal/platform/mapper/mapper_test.go +++ b/internal/platform/mapper/mapper_test.go @@ -4,11 +4,17 @@ import ( "github.com/microsoft/abstrakt/internal/platform/mapper" "github.com/microsoft/abstrakt/internal/tools/guid" "github.com/stretchr/testify/assert" + "io/ioutil" "reflect" "testing" ) func TestMapFromString(t *testing.T) { + contentBytes, err := ioutil.ReadFile("testdata/mapper.yaml") + if nil != err { + t.Fatal(err) + } + type args struct { yamlString string } @@ -20,7 +26,7 @@ func TestMapFromString(t *testing.T) { }{ { name: "Test.01", - args: args{yamlString: configMapTest01String}, + args: args{yamlString: string(contentBytes)}, wantRet: &buildMap01, wantErr: false, }, @@ -47,23 +53,15 @@ func TestMapLoadFile(t *testing.T) { assert.Truef(t, reflect.DeepEqual(&buildMap01, mapper), "Expected: %v\nGot: %v", &buildMap01, mapper) } -const configMapTest01String = ` -Name: "Basic Azure Event Hubs maps" -Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" -Maps: -- ChartName: "event_hub_sample_event_generator" - Type: "EventGenerator" - Location: "../../helm/basictest" - Version: "1.0.0" -- ChartName: "event_hub_sample_event_logger" - Type: "EventLogger" - Location: "../../helm/basictest" - Version: "1.0.0" -- ChartName: "event_hub_sample_event_hub" - Type: "EventHub" - Location: "../../helm/basictest" - Version: "1.0.0" -` +func TestIsEmptyTrue(t *testing.T) { + dag := &mapper.Config{} + assert.True(t, dag.IsEmpty()) +} + +func TestIsEmptyFalse(t *testing.T) { + dag := buildMap01 + assert.False(t, dag.IsEmpty()) +} var buildMap01 = mapper.Config{ Name: "Basic Azure Event Hubs maps", @@ -78,13 +76,13 @@ var buildMap01 = mapper.Config{ { ChartName: "event_hub_sample_event_logger", Type: "EventLogger", - Location: "../../helm/basictest", + Location: "../../helm/basictest2", Version: "1.0.0", }, { ChartName: "event_hub_sample_event_hub", Type: "EventHub", - Location: "../../helm/basictest", + Location: "../../helm/basictest3", Version: "1.0.0", }, }, From 1fc2a4c84cdc96b7d45b174184e2f8113addb4a1 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 20 Jan 2020 16:10:13 +1100 Subject: [PATCH 28/85] Added CMD testdata. --- cmd/testdata/constellation/invalid.yaml | 23 +++++++++++++++++++++++ cmd/testdata/constellation/valid.yaml | 23 +++++++++++++++++++++++ cmd/testdata/mapper/invalid.yaml | 11 +++++++++++ cmd/testdata/mapper/valid.yaml | 15 +++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 cmd/testdata/constellation/invalid.yaml create mode 100644 cmd/testdata/constellation/valid.yaml create mode 100644 cmd/testdata/mapper/invalid.yaml create mode 100644 cmd/testdata/mapper/valid.yaml diff --git a/cmd/testdata/constellation/invalid.yaml b/cmd/testdata/constellation/invalid.yaml new file mode 100644 index 0000000..734dd50 --- /dev/null +++ b/cmd/testdata/constellation/invalid.yaml @@ -0,0 +1,23 @@ +Name: "Azure Event Hubs Sample" +Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" +Services: +- Id: "Event Generator" + Type: "EventGenerator" + Properties: {} +- Id: "Event Generator" + Type: "EventHub" + Properties: {} +- Id: "Event Generator" + Type: "EventLogger" + Properties: {} +Relationships: +- Id: "Generator to Event Hubs Link" + Description: "Event Generator to Event Hub connection" + From: "Event Generator" + To: "Event Generator" + Properties: {} +- Id: "Event Hubs to Event Logger Link" + Description: "Event Hubs to Event Logger connection" + From: "Event Generator" + To: "Event Logger" + Properties: {} diff --git a/cmd/testdata/constellation/valid.yaml b/cmd/testdata/constellation/valid.yaml new file mode 100644 index 0000000..5f9e954 --- /dev/null +++ b/cmd/testdata/constellation/valid.yaml @@ -0,0 +1,23 @@ +Name: "Azure Event Hubs Sample" +Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" +Services: +- Id: "Event Generator" + Type: "EventGenerator" + Properties: {} +- Id: "Azure Event Hub" + Type: "EventHub" + Properties: {} +- Id: "Event Logger" + Type: "EventLogger" + Properties: {} +Relationships: +- Id: "Generator to Event Hubs Link" + Description: "Event Generator to Event Hub connection" + From: "Event Generator" + To: "Azure Event Hub" + Properties: {} +- Id: "Event Hubs to Event Logger Link" + Description: "Event Hubs to Event Logger connection" + From: "Azure Event Hub" + To: "Event Logger" + Properties: {} diff --git a/cmd/testdata/mapper/invalid.yaml b/cmd/testdata/mapper/invalid.yaml new file mode 100644 index 0000000..fdd0c3a --- /dev/null +++ b/cmd/testdata/mapper/invalid.yaml @@ -0,0 +1,11 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest" + Version: "1.0.0" diff --git a/cmd/testdata/mapper/valid.yaml b/cmd/testdata/mapper/valid.yaml new file mode 100644 index 0000000..033bb78 --- /dev/null +++ b/cmd/testdata/mapper/valid.yaml @@ -0,0 +1,15 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "event_hub_sample_event_generator" + Type: "EventGenerator" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_logger" + Type: "EventLogger" + Location: "../../helm/basictest2" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest3" + Version: "1.0.0" From 1c3259f4ebc4b3c14b490b1219be4df477897c8b Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 20 Jan 2020 16:28:30 +1100 Subject: [PATCH 29/85] Added helper to loop through logrus logs. --- internal/tools/helpers/test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/tools/helpers/test.go b/internal/tools/helpers/test.go index 545b433..92a9637 100644 --- a/internal/tools/helpers/test.go +++ b/internal/tools/helpers/test.go @@ -3,6 +3,7 @@ package helpers import ( "bytes" set "github.com/deckarep/golang-set" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "io/ioutil" "os" @@ -100,3 +101,11 @@ func CompareGraphOutputAsSets(expected, produced string) bool { return setProduced.Equal(setExpected) } + +// GetAllLogs loops through logrus entries and returns messages as []string +func GetAllLogs(logs []*logrus.Entry) (entries []string) { + for _, i := range logs { + entries = append(entries, i.Message) + } + return +} From cd179f96bdeff8f788ff190d4c5d2d398905a71f Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 20 Jan 2020 16:29:01 +1100 Subject: [PATCH 30/85] Use testdata for visualise test. --- cmd/visualise_test.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/cmd/visualise_test.go b/cmd/visualise_test.go index ec0006f..3e852af 100644 --- a/cmd/visualise_test.go +++ b/cmd/visualise_test.go @@ -12,9 +12,7 @@ import ( ) func TestVisualiseCmdWithAllRequirementsNoError(t *testing.T) { - constellationPath, _, tdir := helpers.PrepareRealFilesForTest(t) - - defer helpers.CleanTempTestFiles(t, tdir) + constellationPath := "testdata/constellation/valid.yaml" hook := test.NewGlobal() _, err := helpers.ExecuteCommand(newVisualiseCmd().cmd, "-f", constellationPath) @@ -116,13 +114,11 @@ Type: EventGenerator const validGraphString = `digraph Azure_Event_Hubs_Sample { rankdir=LR; - "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8"->"3aa1e546-1ed5-4d67-a59c-be0d5905b490"; - "3aa1e546-1ed5-4d67-a59c-be0d5905b490"->"1d0255d4-5b8c-4a52-b0bb-ac024cda37e5"; - "3aa1e546-1ed5-4d67-a59c-be0d5905b490"->"a268fae5-2a82-4a3e-ada7-a52eeb7019ac"; - "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5"; - "3aa1e546-1ed5-4d67-a59c-be0d5905b490"; - "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8"; - "a268fae5-2a82-4a3e-ada7-a52eeb7019ac"; + "Event_Generator"->"Azure_Event_Hub"; + "Azure_Event_Hub"->"Event_Logger"; + "Azure_Event_Hub"; + "Event_Generator"; + "Event_Logger"; } ` From 42844355c6e5cba88ef2240b441afe9b6fba44c6 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 20 Jan 2020 16:29:28 +1100 Subject: [PATCH 31/85] Updated validate to validate both constellation and mapper. --- cmd/validate.go | 139 ++++++++++++++----- cmd/validate_test.go | 141 +++++++++++++++++++- internal/platform/constellation/validate.go | 4 +- 3 files changed, 242 insertions(+), 42 deletions(-) diff --git a/cmd/validate.go b/cmd/validate.go index c115cbd..5ec765c 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/platform/mapper" + "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/microsoft/abstrakt/internal/tools/logger" "github.com/spf13/cobra" ) @@ -20,41 +21,57 @@ func newValidateCmd() *validateCmd { cc.baseCmd = newBaseCmd(&cobra.Command{ Use: "validate", Short: "Validate a constellation file for correct schema and ensure correctness.", - Long: `Validate is used to ensure the correctness of a constellation file. + Long: `Validate is used to ensure the correctness of a constellation and mapper files. -Example: abstrakt validate -f [constellationFilePath]`, +Example: abstrakt validate -f [constellationFilePath] -m [mapperFilePath] + abstrakt validate -f [constellationFilePath] + abstrakt validate -m [mapperFilePath]`, SilenceUsage: true, SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) (err error) { if len(cc.constellationFilePath) == 0 && len(cc.mapperFilePath) == 0 { - return fmt.Errorf("No arguments passed") + cc.baseCmd.cmd.Usage() + return fmt.Errorf("no flags were set") } + var d constellation.Config + var m mapper.Config + fail := false if len(cc.mapperFilePath) > 0 { - err = loadAndTestMapper(cc.mapperFilePath) + m, err = loadAndValidateMapper(cc.mapperFilePath) if err != nil { - logger.Errorf("Mapper: %v", err) + logger.Errorf("mapper: %v", err) fail = true } else { - logger.Info("Mapper: valid") + logger.Info("mapper: valid") } } if len(cc.constellationFilePath) > 0 { - err = loadAndTestDag(cc.constellationFilePath) + d, err = loadAndValidateDag(cc.constellationFilePath) if err != nil { - logger.Errorf("Constellation: %v", err) + logger.Errorf("constellation: %v", err) fail = true } else { - logger.Info("Constellation: valid") + logger.Info("constellation: valid") + } + } + + if !d.IsEmpty() && !m.IsEmpty() { + err = validateDagAndMapper(&d, &m) + if err != nil { + logger.Errorf("deployment: %v", err) + fail = true + } else { + logger.Info("deployment: valid") } } if fail { - err = fmt.Errorf("Invalid configuration(s)") + err = fmt.Errorf("invalid configuration(s)") } return @@ -67,77 +84,133 @@ Example: abstrakt validate -f [constellationFilePath]`, return cc } -func loadAndTestDag(path string) (err error) { - d := new(constellation.Config) - err = d.LoadFile(path) +func validateDagAndMapper(d *constellation.Config, m *mapper.Config) (err error) { + types := []string{} + mapTypes := []string{} + + for _, i := range d.Services { + _, exists := helpers.Find(types, i.Type) + if !exists { + types = append(types, i.Type) + } + } + + for _, i := range m.Maps { + mapTypes = append(mapTypes, i.Type) + } + + logger.Debug("deployment: checking if `Service` exists in map") + for _, i := range types { + _, exists := helpers.Find(mapTypes, i) + if !exists { + logger.Error("missing map configuration(s)") + logger.Errorf("service `%v` does not exist in map", i) + err = fmt.Errorf("invalid") + } + } + + return +} + +func loadAndValidateDag(path string) (config constellation.Config, err error) { + err = config.LoadFile(path) if err != nil { return } - return validateDag(d) + return config, validateDag(&config) } // validateDag takes a constellation dag and returns any errors. -func validateDag(dag *constellation.Config) (err error) { - err = dag.ValidateModel() +func validateDag(d *constellation.Config) (err error) { + logger.Debug("constellation: validating schema") + err = d.ValidateModel() if err != nil { - return + logger.Debug(err) + return fmt.Errorf("invalid schema") } - duplicates := dag.CheckDuplicates() + logger.Debug("constellation: checking for duplicate `ID`") + duplicates := d.DuplicateIDs() if duplicates != nil { - logger.Error("Duplicate IDs found:") + logger.Error("duplicate `ID` present in config") for _, i := range duplicates { logger.Errorf("'%v'", i) } - err = error(fmt.Errorf("Constellation is invalid")) + err = fmt.Errorf("invalid") } - connections := dag.CheckServiceExists() + logger.Debug("constellation: checking if `Service` exists") + connections := d.CheckServiceExists() if len(connections) > 0 { + logger.Error("missing relationship(s)") for key, i := range connections { - logger.Errorf("Relationship '%v' has missing Services:", key) + logger.Errorf("relationship '%v' has missing `Services`:", key) for _, j := range i { logger.Errorf("'%v'", j) } } - err = error(fmt.Errorf("Constellation is invalid")) + err = fmt.Errorf("invalid") } return } -func loadAndTestMapper(path string) (err error) { - m := new(mapper.Config) - err = m.LoadFile(path) +func loadAndValidateMapper(path string) (config mapper.Config, err error) { + err = config.LoadFile(path) if err != nil { return } - return validateMapper(m) + return config, validateMapper(&config) } // validateMapper takes a constellation mapper and returns any errors. -func validateMapper(mapper *mapper.Config) (err error) { - err = mapper.ValidateModel() +func validateMapper(m *mapper.Config) (err error) { + logger.Debug("mapper: validating schema") + err = m.ValidateModel() if err != nil { - return + logger.Debug(err) + return fmt.Errorf("invalid schema") } - duplicates := mapper.CheckDuplicates() + logger.Debug("mapper: checking for duplicate `ChartName`") + duplicates := m.DuplicateChartName() if duplicates != nil { - logger.Error("Duplicate IDs found:") + logger.Error("duplicate `ChartName` present in config") for _, i := range duplicates { logger.Errorf("'%v'", i) } - err = error(fmt.Errorf("Constellation is invalid")) + err = fmt.Errorf("invalid") + } + + logger.Debug("mapper: checking for duplicate `Type`") + duplicates = m.DuplicateType() + + if duplicates != nil { + logger.Error("duplicate `Type` present in config") + for _, i := range duplicates { + logger.Errorf("'%v'", i) + } + err = fmt.Errorf("invalid") + } + + logger.Debug("mapper: checking for duplicate `Location`") + duplicates = m.DuplicateLocation() + + if duplicates != nil { + logger.Error("duplicate `Location` present in config") + for _, i := range duplicates { + logger.Errorf("'%v'", i) + } + err = fmt.Errorf("invalid") } return diff --git a/cmd/validate_test.go b/cmd/validate_test.go index f851e84..a71d943 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -2,26 +2,153 @@ package cmd import ( "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" "testing" ) -func TestValidateCommand(t *testing.T) { - expected := "open does-not-exist: no such file or directory" +func TestValidateCommandNoArgs(t *testing.T) { + _, err := helpers.ExecuteCommand(newValidateCmd().cmd) + assert.Error(t, err) + assert.EqualError(t, err, "no flags were set") +} - constellationPath, _, tdir := helpers.PrepareRealFilesForTest(t) +func TestValidateCommandConstellationAndMapper(t *testing.T) { + constellationPath := "testdata/constellation/valid.yaml" + mapPath := "testdata/mapper/valid.yaml" - defer helpers.CleanTempTestFiles(t, tdir) + output, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath, "-m", mapPath) + assert.NoErrorf(t, err, "Did not received expected error. \nGot:\n %v", output) +} + +func TestValidateCommandConstellationExist(t *testing.T) { + constellationPath := "testdata/constellation/valid.yaml" output, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) - assert.NoErrorf(t, err, "Did not received expected error. \nGot:\n %v", output) +} - _, err = helpers.ExecuteCommand(newValidateCmd().cmd, "-f", "does-not-exist") +func TestValidateCommandMapExist(t *testing.T) { + mapPath := "testdata/mapper/valid.yaml" + + output, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-m", mapPath) + assert.NoErrorf(t, err, "Did not received expected error. \nGot:\n %v", output) +} + +func TestValidateCommandConstellationFail(t *testing.T) { + expected := "constellation: open does-not-exist: no such file or directory" + + hook := test.NewGlobal() + _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", "does-not-exist") + + entries := []string{} + + for _, i := range hook.AllEntries() { + entries = append(entries, i.Message) + } if err != nil { - assert.Contains(t, err.Error(), expected) + assert.Contains(t, entries, expected) } else { t.Errorf("Did not received expected error. \nExpected: %v\nGot:\n %v", expected, err.Error()) } } + +func TestValidateCommandMapFail(t *testing.T) { + expected := "mapper: open does-not-exist: no such file or directory" + + hook := test.NewGlobal() + _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-m", "does-not-exist") + + entries := helpers.GetAllLogs(hook.AllEntries()) + + if err != nil { + assert.Contains(t, entries, expected) + } else { + t.Errorf("Did not received expected error. \nExpected: %v\nGot:\n %v", expected, err.Error()) + } +} + +func TestValidateCommandConstellationInvalidSchema(t *testing.T) { + mapPath := "testdata/mapper/valid.yaml" + + hook := test.NewGlobal() + _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", mapPath) + + entries := helpers.GetAllLogs(hook.AllEntries()) + + assert.Error(t, err) + assert.Contains(t, entries, "constellation: invalid schema") + assert.EqualError(t, err, "invalid configuration(s)") +} + +func TestValidateCommandNapperInvalidSchema(t *testing.T) { + constellationPath := "testdata/constellation/valid.yaml" + + hook := test.NewGlobal() + _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-m", constellationPath) + + entries := helpers.GetAllLogs(hook.AllEntries()) + + assert.Error(t, err) + assert.Contains(t, entries, "mapper: invalid schema") + assert.EqualError(t, err, "invalid configuration(s)") +} + +func TestValidateDeploymentFail(t *testing.T) { + constellationPath := "testdata/constellation/valid.yaml" + mapPath := "testdata/mapper/invalid.yaml" + + hook := test.NewGlobal() + _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath, "-m", mapPath) + + entries := helpers.GetAllLogs(hook.AllEntries()) + + assert.Error(t, err) + assert.Contains(t, entries, "service `EventLogger` does not exist in map") + assert.EqualError(t, err, "invalid configuration(s)") +} + +func TestValidateMapperDuplicates(t *testing.T) { + mapPath := "testdata/mapper/invalid.yaml" + + hook := test.NewGlobal() + _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-m", mapPath) + + entries := helpers.GetAllLogs(hook.AllEntries()) + + assert.Error(t, err) + assert.Contains(t, entries, "duplicate `ChartName` present in config") + assert.Contains(t, entries, "duplicate `Type` present in config") + assert.Contains(t, entries, "duplicate `Location` present in config") + assert.Contains(t, entries, "mapper: invalid") + assert.EqualError(t, err, "invalid configuration(s)") +} + +func TestValidateConstellationDuplicateIDs(t *testing.T) { + constellationPath := "testdata/constellation/invalid.yaml" + + hook := test.NewGlobal() + _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) + + entries := helpers.GetAllLogs(hook.AllEntries()) + + assert.Error(t, err) + assert.Contains(t, entries, "duplicate `ID` present in config") + assert.Contains(t, entries, "constellation: invalid") + assert.EqualError(t, err, "invalid configuration(s)") +} + +func TestValidateConstellationMissingServices(t *testing.T) { + constellationPath := "testdata/constellation/invalid.yaml" + + hook := test.NewGlobal() + _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) + + entries := helpers.GetAllLogs(hook.AllEntries()) + + assert.Error(t, err) + assert.Contains(t, entries, "relationship 'Event Hubs to Event Logger Link' has missing `Services`:") + assert.Contains(t, entries, "constellation: invalid") + assert.EqualError(t, err, "invalid configuration(s)") +} diff --git a/internal/platform/constellation/validate.go b/internal/platform/constellation/validate.go index 045128f..60aadf7 100644 --- a/internal/platform/constellation/validate.go +++ b/internal/platform/constellation/validate.go @@ -5,8 +5,8 @@ import ( "gopkg.in/dealancer/validate.v2" ) -// CheckDuplicates checks for duplicate Relationship and Service IDs in a constellation file. -func (m *Config) CheckDuplicates() (duplicates []string) { +// DuplicateIDs checks for duplicate Relationship and Service IDs in a constellation file. +func (m *Config) DuplicateIDs() (duplicates []string) { IDs := []string{string(m.ID)} for _, i := range m.Services { From 8f2a6f9a1dead08b3546e19c64045ec0bbcd43b9 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 20 Jan 2020 16:32:23 +1100 Subject: [PATCH 32/85] Fixed tests. --- internal/platform/constellation/validate_test.go | 8 ++++---- internal/platform/mapper/find_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/platform/constellation/validate_test.go b/internal/platform/constellation/validate_test.go index d66e3a9..0d5f950 100644 --- a/internal/platform/constellation/validate_test.go +++ b/internal/platform/constellation/validate_test.go @@ -12,7 +12,7 @@ func TestForDuplicatIDsInServices(t *testing.T) { err := testData.LoadFile("testdata/valid.yaml") assert.NoError(t, err) - duplicates := testData.CheckDuplicates() + duplicates := testData.DuplicateIDs() assert.Nil(t, duplicates, "No duplicates should be found.") } @@ -42,7 +42,7 @@ func TestForDuplicatIDsInServicesFail(t *testing.T) { err := testData.LoadFile("testdata/duplicate/servIds.yaml") assert.NoError(t, err) - duplicates := testData.CheckDuplicates() + duplicates := testData.DuplicateIDs() assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 2) assert.Equal(t, 2, len(duplicates)) @@ -54,7 +54,7 @@ func TestForDuplicatIDsInRelationshipsFail(t *testing.T) { err := testData.LoadFile("testdata/duplicate/relIds.yaml") assert.NoError(t, err) - duplicates := testData.CheckDuplicates() + duplicates := testData.DuplicateIDs() assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 1) assert.Equal(t, 1, len(duplicates)) @@ -66,7 +66,7 @@ func TestForDuplicatIDsFail(t *testing.T) { err := testData.LoadFile("testdata/duplicate/servRelIds.yaml") assert.NoError(t, err) - duplicates := testData.CheckDuplicates() + duplicates := testData.DuplicateIDs() assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 1) assert.Equal(t, 1, len(duplicates)) diff --git a/internal/platform/mapper/find_test.go b/internal/platform/mapper/find_test.go index 2bce287..ec2b167 100644 --- a/internal/platform/mapper/find_test.go +++ b/internal/platform/mapper/find_test.go @@ -25,7 +25,7 @@ func TestFindByName(t *testing.T) { assert.NotNil(t, info) assert.Equal(t, "event_hub_sample_event_logger", info.ChartName) assert.Equal(t, "EventLogger", info.Type) - assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "../../helm/basictest2", info.Location) assert.Equal(t, "1.0.0", info.Version) info = mapper.FindByName("event_hub_sample_event_hub") @@ -33,7 +33,7 @@ func TestFindByName(t *testing.T) { assert.NotNil(t, info) assert.Equal(t, "event_hub_sample_event_hub", info.ChartName) assert.Equal(t, "EventHub", info.Type) - assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "../../helm/basictest3", info.Location) assert.Equal(t, "1.0.0", info.Version) } @@ -56,7 +56,7 @@ func TestFindByType(t *testing.T) { assert.NotNil(t, info) assert.Equal(t, "event_hub_sample_event_logger", info.ChartName) assert.Equal(t, "EventLogger", info.Type) - assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "../../helm/basictest2", info.Location) assert.Equal(t, "1.0.0", info.Version) info = mapper.FindByType("EventHub") @@ -64,6 +64,6 @@ func TestFindByType(t *testing.T) { assert.NotNil(t, info) assert.Equal(t, "event_hub_sample_event_hub", info.ChartName) assert.Equal(t, "EventHub", info.Type) - assert.Equal(t, "../../helm/basictest", info.Location) + assert.Equal(t, "../../helm/basictest3", info.Location) assert.Equal(t, "1.0.0", info.Version) } From 2ca7c7f6f11e951a97e8984beb32049205cd966a Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 21 Jan 2020 09:37:44 +1100 Subject: [PATCH 33/85] Added additional tests. --- internal/compose/compose.go | 3 +-- internal/tools/helpers/file_test.go | 20 ++++++++++++++++++++ internal/tools/helpers/find_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 internal/tools/helpers/file_test.go create mode 100644 internal/tools/helpers/find_test.go diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 0cacb81..193c524 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -1,7 +1,6 @@ package compose import ( - "errors" "fmt" "github.com/microsoft/abstrakt/internal/platform/chart" "github.com/microsoft/abstrakt/internal/platform/constellation" @@ -18,7 +17,7 @@ type Composer struct { //Build takes the loaded DAG and maps and builds the Helm values and requirements documents func (m *Composer) Build(name string, dir string) (*helm.Chart, error) { if m.Constellation.Name == "" || m.Mapper.Name == "" { - return nil, errors.New("Please initialise with LoadFromFile or LoadFromString") + return nil, fmt.Errorf("Please initialise with LoadFromFile or LoadFromString") } newChart, err := chart.Create(name, dir) diff --git a/internal/tools/helpers/file_test.go b/internal/tools/helpers/file_test.go new file mode 100644 index 0000000..22c832b --- /dev/null +++ b/internal/tools/helpers/file_test.go @@ -0,0 +1,20 @@ +package helpers_test + +import ( + "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestFileExistsNoFile(t *testing.T) { + assert.False(t, helpers.FileExists("does-not-exist")) +} + +func TestFileExists(t *testing.T) { + assert.True(t, helpers.FileExists("../../../README.md")) +} + +func TestFileExistsFolder(t *testing.T) { + assert.False(t, helpers.FileExists("../helpers")) + assert.False(t, helpers.FileExists("../helpers/")) +} diff --git a/internal/tools/helpers/find_test.go b/internal/tools/helpers/find_test.go new file mode 100644 index 0000000..7b033d1 --- /dev/null +++ b/internal/tools/helpers/find_test.go @@ -0,0 +1,26 @@ +package helpers_test + +import ( + "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestFindElementInSlice(t *testing.T) { + slice := []string{"a", "b", "c", "d", "e", "f"} + + index, found := helpers.Find(slice, "d") + + assert.True(t, found) + assert.NotEqual(t, -1, index) + assert.Equal(t, 3, index) +} + +func TestFindElementNotInSlice(t *testing.T) { + slice := []string{"a", "b", "c", "d", "e", "f"} + + index, found := helpers.Find(slice, "g") + + assert.False(t, found) + assert.Equal(t, -1, index) +} From 15ab9aec14e2a5bea65183df7e15f7aa35d320a3 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 22 Jan 2020 10:48:15 +1100 Subject: [PATCH 34/85] Fixed golangci.yml. --- .golangci.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a162de3..b29107e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,18 +2,18 @@ run: deadline: 5m skip-files: [] linters-settings: - linters-settings.govet: + govet: check-shadowing: true - linters-settings.gocyclo: + gocyclo: min-complexity: 12.0 - linters-settings.maligned: + maligned: suggest-new: true - linters-settings.goconst: + goconst: min-len: 3.0 min-occurrences: 3.0 - linters-settings.misspell: + misspell: locale: "US" - ignore-words: + ignore-words: - listend - analyses - cancelling From 2ad09482635b6c7500091ae107d420f69d545381 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 22 Jan 2020 10:53:06 +1100 Subject: [PATCH 35/85] Fixed liniting issues. --- cmd/commands.go | 2 +- cmd/diff.go | 10 ++++---- cmd/validate.go | 2 +- cmd/{visualise.go => visualize.go} | 16 ++++++------- cmd/{visualise_test.go => visualize_test.go} | 15 ++++++------ docs/usage.md | 24 ++++++++++---------- internal/compose/compose.go | 2 +- internal/platform/constellation/diff.go | 8 +++---- internal/platform/constellation/graph.go | 2 +- makefile | 4 ++-- 10 files changed, 43 insertions(+), 42 deletions(-) rename cmd/{visualise.go => visualize.go} (81%) rename cmd/{visualise_test.go => visualize_test.go} (88%) diff --git a/cmd/commands.go b/cmd/commands.go index 37391a7..a110076 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -52,7 +52,7 @@ func DefaultRootCommand() *cobra.Command { return nil } - addCommands(c, newComposeCmd(), newVersionCmd(), newVisualiseCmd(), newValidateCmd(), newDiffCmd()) + addCommands(c, newComposeCmd(), newVersionCmd(), newVisualizeCmd(), newValidateCmd(), newDiffCmd()) return c } diff --git a/cmd/diff.go b/cmd/diff.go index 7b37ddf..39efaab 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -1,9 +1,9 @@ package cmd -// visualise is a subcommand that constructs a graph representation of the yaml +// visualize is a subcommand that constructs a graph representation of the yaml // input file and renders this into GraphViz 'dot' notation. // Initial version renders to dot syntax only, to graphically depict this the output -// has to be run through a graphviz visualisation tool/utiliyy +// has to be run through a graphviz visualization tool/utiliyy import ( "bytes" @@ -57,7 +57,8 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP if *cc.showOriginal { out := &bytes.Buffer{} - resStringOrg, err := dsGraphOrg.GenerateGraph(out) + var resStringOrg string + resStringOrg, err = dsGraphOrg.GenerateGraph(out) if err != nil { return err } @@ -72,7 +73,8 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP if *cc.showNew { out := &bytes.Buffer{} - resStringNew, err := dsGraphNew.GenerateGraph(out) + var resStringNew string + resStringNew, err = dsGraphNew.GenerateGraph(out) if err != nil { return err } diff --git a/cmd/validate.go b/cmd/validate.go index 5ec765c..aec8b03 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -31,7 +31,7 @@ Example: abstrakt validate -f [constellationFilePath] -m [mapperFilePath] RunE: func(cmd *cobra.Command, args []string) (err error) { if len(cc.constellationFilePath) == 0 && len(cc.mapperFilePath) == 0 { - cc.baseCmd.cmd.Usage() + _ = cc.baseCmd.cmd.Usage() return fmt.Errorf("no flags were set") } diff --git a/cmd/visualise.go b/cmd/visualize.go similarity index 81% rename from cmd/visualise.go rename to cmd/visualize.go index d3f7033..4110280 100644 --- a/cmd/visualise.go +++ b/cmd/visualize.go @@ -1,9 +1,9 @@ package cmd -// visualise is a subcommand that constructs a graph representation of the yaml +// visualize is a subcommand that constructs a graph representation of the yaml // input file and renders this into GraphViz 'dot' notation. // Initial version renders to dot syntax only, to graphically depict this the output -// has to be run through a graphviz visualisation tool/utiliyy +// has to be run through a graphviz visualization tool/utiliyy import ( "bytes" @@ -15,20 +15,20 @@ import ( "strings" ) -type visualiseCmd struct { +type visualizeCmd struct { constellationFilePath string *baseCmd } -func newVisualiseCmd() *visualiseCmd { - cc := &visualiseCmd{} +func newVisualizeCmd() *visualizeCmd { + cc := &visualizeCmd{} cc.baseCmd = newBaseCmd(&cobra.Command{ - Use: "visualise", + Use: "visualize", Short: "Format a constellation configuration as Graphviz dot notation", - Long: `Visualise is for producing Graphviz dot notation code of a constellation configuration + Long: `Visualize is for producing Graphviz dot notation code of a constellation configuration -Example: abstrakt visualise -f [constellationFilePath]`, +Example: abstrakt visualize -f [constellationFilePath]`, RunE: func(cmd *cobra.Command, args []string) error { logger.Debug("args: " + strings.Join(args, " ")) diff --git a/cmd/visualise_test.go b/cmd/visualize_test.go similarity index 88% rename from cmd/visualise_test.go rename to cmd/visualize_test.go index 3e852af..b3064bd 100644 --- a/cmd/visualise_test.go +++ b/cmd/visualize_test.go @@ -11,11 +11,11 @@ import ( "testing" ) -func TestVisualiseCmdWithAllRequirementsNoError(t *testing.T) { +func TestVisualizeCmdWithAllRequirementsNoError(t *testing.T) { constellationPath := "testdata/constellation/valid.yaml" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newVisualiseCmd().cmd, "-f", constellationPath) + _, err := helpers.ExecuteCommand(newVisualizeCmd().cmd, "-f", constellationPath) if err != nil { t.Error("Did not receive output") @@ -24,10 +24,10 @@ func TestVisualiseCmdWithAllRequirementsNoError(t *testing.T) { } } -func TestVisualiseCmdFailYaml(t *testing.T) { +func TestVisualizeCmdFailYaml(t *testing.T) { expected := "Could not open YAML input file for reading" - output, err := helpers.ExecuteCommand(newVisualiseCmd().cmd, "-f", "constellationPath") + output, err := helpers.ExecuteCommand(newVisualizeCmd().cmd, "-f", "constellationPath") if err != nil { assert.Contains(t, err.Error(), expected) @@ -36,10 +36,10 @@ func TestVisualiseCmdFailYaml(t *testing.T) { } } -func TestVisualiseCmdFailNotYaml(t *testing.T) { +func TestVisualizeCmdFailNotYaml(t *testing.T) { expected := "dagConfigService failed to load file" - output, err := helpers.ExecuteCommand(newVisualiseCmd().cmd, "-f", "visualise.go") + output, err := helpers.ExecuteCommand(newVisualizeCmd().cmd, "-f", "visualize.go") if err != nil { assert.Contains(t, err.Error(), expected) @@ -49,13 +49,12 @@ func TestVisualiseCmdFailNotYaml(t *testing.T) { } func TestFileExists(t *testing.T) { - _, _, tdir := helpers.PrepareRealFilesForTest(t) defer helpers.CleanTempTestFiles(t, tdir) //Setup variables and content for test - testValidFilename := filepath.Join(tdir, "testVisualise.out") + testValidFilename := filepath.Join(tdir, "testVisualize.out") testInvalidFilename := filepath.Join(tdir, "nonexistant.out") testData := []byte("A file to test with") diff --git a/docs/usage.md b/docs/usage.md index 5fb33a1..ea7da73 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -15,7 +15,7 @@ Available Commands: help Help about any command validate Validate a constellation file for correct schema and ensure correctness. version The version of Abstrakt being used - visualise Format a constellation configuration as Graphviz dot notation + visualize Format a constellation configuration as Graphviz dot notation Flags: -h, --help help for abstrakt @@ -80,27 +80,27 @@ Global Flags: -v, --verbose Use verbose output logs ``` -### abstrakt `visualise` +### abstrakt `visualize` ```bash -Visualise is for producing Graphviz dot notation code of a constellation configuration +Visualize is for producing Graphviz dot notation code of a constellation configuration -Example: abstrakt visualise -f [constellationFilePath] +Example: abstrakt visualize -f [constellationFilePath] Usage: - abstrakt visualise [flags] + abstrakt visualize [flags] Flags: -f, --constellationFilePath string constellation file path - -h, --help help for visualise + -h, --help help for visualize Global Flags: -v, --verbose Use verbose output logs ``` -The output from the visualise subcommand is [Graphviz dot notation](https://www.graphviz.org/doc/info/lang.html) +The output from the visualize subcommand is [Graphviz dot notation](https://www.graphviz.org/doc/info/lang.html) -The output from a call to 'abstrakt visualise' can be piped into Graphviz to generate a graphical output. See the example in the Examples section. +The output from a call to 'abstrakt visualize' can be piped into Graphviz to generate a graphical output. See the example in the Examples section. Alternatively, copy the output and paste into a Graphviz rendering tool to see the graph produced. Some sites listed below (rendering option in the utility to be developed). @@ -129,9 +129,9 @@ Global Flags: #### Examples -Run visualise on a file +Run visualize on a file - abstrakt visualise -f basic_azure_event_hubs.yaml + abstrakt visualize -f basic_azure_event_hubs.yaml digraph Azure_Event_Hubs_Sample { Event_Generator->Azure_Event_Hub; Azure_Event_Hub->Event_Logger; @@ -141,6 +141,6 @@ Run visualise on a file } -Pipe visualise output to Graphviz producing a file called result.png (assumes Graphviz is installed and can be called from the location abstrakt is being run) +Pipe visualize output to Graphviz producing a file called result.png (assumes Graphviz is installed and can be called from the location abstrakt is being run) - abstrakt visualise -f ./examples/constellation/sample_consteallation.yaml | dot -Tpng > result.png \ No newline at end of file + abstrakt visualize -f ./examples/constellation/sample_consteallation.yaml | dot -Tpng > result.png \ No newline at end of file diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 193c524..a6ef117 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -17,7 +17,7 @@ type Composer struct { //Build takes the loaded DAG and maps and builds the Helm values and requirements documents func (m *Composer) Build(name string, dir string) (*helm.Chart, error) { if m.Constellation.Name == "" || m.Mapper.Name == "" { - return nil, fmt.Errorf("Please initialise with LoadFromFile or LoadFromString") + return nil, fmt.Errorf("Please initialize with LoadFromFile or LoadFromString") } newChart, err := chart.Create(name, dir) diff --git a/internal/platform/constellation/diff.go b/internal/platform/constellation/diff.go index 232b78a..ac5a230 100644 --- a/internal/platform/constellation/diff.go +++ b/internal/platform/constellation/diff.go @@ -67,10 +67,10 @@ func createSet(dsGraph *Config) (set.Set, set.Set) { } // CreateGraphWithChanges - use both input constellations (new and original) as well as the comparison sets to create -// a dag that can be visualised. It uses the comparison sets to identify additions, deletions and changes between the original +// a dag that can be visualized. It uses the comparison sets to identify additions, deletions and changes between the original // and new constellations. func CreateGraphWithChanges(newGraph *Config, sets *ComparisonSet) (string, error) { - // Lookup is used to map IDs to names. Names are easier to visualise but IDs are more important to ensure the + // Lookup is used to map IDs to names. Names are easier to visualize but IDs are more important to ensure the // presented constellation is correct and IDs are used to link nodes together lookup := make(map[string]string) g := gographviz.NewGraph() @@ -90,7 +90,7 @@ func CreateGraphWithChanges(newGraph *Config, sets *ComparisonSet) (string, erro } // Add all services from the new constellation - // - New services - highlight with colour (i.e in setAddedSvcs) + // - New services - highlight with color (i.e in setAddedSvcs) // - Deleted services (i.e. in setDelSvcs) - include and format appropriately for _, v := range newGraph.Services { newName := strings.Replace(v.ID, " ", "_", -1) // Replace spaces in names with underscores, names with spaces can break graphviz engines) @@ -128,7 +128,7 @@ func CreateGraphWithChanges(newGraph *Config, sets *ComparisonSet) (string, erro //======================================== process relationships ========================================== // Add Relationships from the new constellation - // - New relationships - highlight with colour (i.e. in setAddedSvcs) + // - New relationships - highlight with color (i.e. in setAddedSvcs) // - Deleted relationships (i.e. in setDelRels) - include and format appropriately for _, v := range newGraph.Relationships { //Surround names/labels with quotes, stops graphviz seeing special characters and breaking diff --git a/internal/platform/constellation/graph.go b/internal/platform/constellation/graph.go index 46fe32a..8763866 100644 --- a/internal/platform/constellation/graph.go +++ b/internal/platform/constellation/graph.go @@ -12,7 +12,7 @@ import ( // this can be passed on to GraphViz to graphically render the resulting graph func (readGraph *Config) GenerateGraph(out io.Writer) (string, error) { - // Lookup is used to map IDs to names. Names are easier to visualise but IDs are more important to ensure the + // Lookup is used to map IDs to names. Names are easier to visualize but IDs are more important to ensure the // presented constellation is correct and IDs are used to link nodes together lookup := make(map[string]string) diff --git a/makefile b/makefile index ae0dd35..7b9f11c 100644 --- a/makefile +++ b/makefile @@ -99,8 +99,8 @@ fmt: build: go build -o abstrakt main.go -visualise: build - ./abstrakt visualise -f ./examples/constellation/http_constellation.yaml | dot -Tpng > result.png +visualize: build + ./abstrakt visualize -f ./examples/constellation/http_constellation.yaml | dot -Tpng > result.png diff: build ./abstrakt diff -o ./examples/constellation/sample_constellation.yaml -n ./examples/constellation/sample_constellation_changed.yaml | dot -Tpng > result.png From 819f5d6d2a9f9cb34161f319e082cdabb6a9bd46 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 22 Jan 2020 13:37:40 +1100 Subject: [PATCH 36/85] Fixed gopls issue. --- .devcontainer/Dockerfile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 5d6bae8..7cc4608 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -3,7 +3,7 @@ # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. #------------------------------------------------------------------------------------------------------------- -FROM golang:1.13.1 +FROM golang:1.13.1-stretch # Avoid warnings by switching to noninteractive ENV DEBIAN_FRONTEND=noninteractive @@ -18,6 +18,7 @@ RUN apt-get update \ && go get -x -d github.com/stamblerre/gocode 2>&1 \ && go build -o gocode-gomod github.com/stamblerre/gocode \ && mv gocode-gomod $GOPATH/bin/ \ + && GO111MODULE=on go get -v golang.org/x/tools/gopls@latest \ && go get -u -v \ github.com/google/wire/cmd/wire \ github.com/rakyll/gotest \ @@ -38,7 +39,6 @@ RUN apt-get update \ github.com/cweill/gotests/... \ golang.org/x/tools/cmd/goimports \ golang.org/x/lint/golint \ - golang.org/x/tools/cmd/gopls \ github.com/alecthomas/gometalinter \ honnef.co/go/tools/... \ github.com/mgechev/revive \ @@ -48,7 +48,6 @@ RUN apt-get update \ && apt-get autoremove -y \ && apt-get clean -y \ && rm -rf /var/lib/apt/lists/* - RUN apt-get update \ # Install Docker CE CLI From 2d57c9a2fc7070dda24ff0aaaa6b0c7ea286e523 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 22 Jan 2020 15:31:19 +1100 Subject: [PATCH 37/85] Adjusted log messages. --- cmd/validate.go | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/cmd/validate.go b/cmd/validate.go index aec8b03..c3ed9fa 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -43,35 +43,35 @@ Example: abstrakt validate -f [constellationFilePath] -m [mapperFilePath] if len(cc.mapperFilePath) > 0 { m, err = loadAndValidateMapper(cc.mapperFilePath) if err != nil { - logger.Errorf("mapper: %v", err) + logger.Errorf("Mapper: %v", err) fail = true } else { - logger.Info("mapper: valid") + logger.Info("Mapper: valid") } } if len(cc.constellationFilePath) > 0 { d, err = loadAndValidateDag(cc.constellationFilePath) if err != nil { - logger.Errorf("constellation: %v", err) + logger.Errorf("Constellation: %v", err) fail = true } else { - logger.Info("constellation: valid") + logger.Info("Constellation: valid") } } if !d.IsEmpty() && !m.IsEmpty() { err = validateDagAndMapper(&d, &m) if err != nil { - logger.Errorf("deployment: %v", err) + logger.Errorf("Deployment: %v", err) fail = true } else { - logger.Info("deployment: valid") + logger.Info("Deployment: valid") } } if fail { - err = fmt.Errorf("invalid configuration(s)") + err = fmt.Errorf("Invalid configuration(s)") } return @@ -103,8 +103,8 @@ func validateDagAndMapper(d *constellation.Config, m *mapper.Config) (err error) for _, i := range types { _, exists := helpers.Find(mapTypes, i) if !exists { - logger.Error("missing map configuration(s)") - logger.Errorf("service `%v` does not exist in map", i) + logger.Error("Missing map configuration(s)") + logger.Errorf("Service `%v` does not exist in map", i) err = fmt.Errorf("invalid") } } @@ -124,7 +124,7 @@ func loadAndValidateDag(path string) (config constellation.Config, err error) { // validateDag takes a constellation dag and returns any errors. func validateDag(d *constellation.Config) (err error) { - logger.Debug("constellation: validating schema") + logger.Debug("Constellation: validating schema") err = d.ValidateModel() if err != nil { @@ -136,20 +136,20 @@ func validateDag(d *constellation.Config) (err error) { duplicates := d.DuplicateIDs() if duplicates != nil { - logger.Error("duplicate `ID` present in config") + logger.Error("Duplicate `ID` present in config") for _, i := range duplicates { logger.Errorf("'%v'", i) } err = fmt.Errorf("invalid") } - logger.Debug("constellation: checking if `Service` exists") + logger.Debug("Constellation: checking if `Service` exists") connections := d.CheckServiceExists() if len(connections) > 0 { - logger.Error("missing relationship(s)") + logger.Error("Missing relationship(s)") for key, i := range connections { - logger.Errorf("relationship '%v' has missing `Services`:", key) + logger.Errorf("Relationship '%v' has missing `Services`:", key) for _, j := range i { logger.Errorf("'%v'", j) } @@ -172,7 +172,7 @@ func loadAndValidateMapper(path string) (config mapper.Config, err error) { // validateMapper takes a constellation mapper and returns any errors. func validateMapper(m *mapper.Config) (err error) { - logger.Debug("mapper: validating schema") + logger.Debug("Mapper: validating schema") err = m.ValidateModel() if err != nil { @@ -180,33 +180,33 @@ func validateMapper(m *mapper.Config) (err error) { return fmt.Errorf("invalid schema") } - logger.Debug("mapper: checking for duplicate `ChartName`") + logger.Debug("Mapper: checking for duplicate `ChartName`") duplicates := m.DuplicateChartName() if duplicates != nil { - logger.Error("duplicate `ChartName` present in config") + logger.Error("Duplicate `ChartName` present in config") for _, i := range duplicates { logger.Errorf("'%v'", i) } err = fmt.Errorf("invalid") } - logger.Debug("mapper: checking for duplicate `Type`") + logger.Debug("Mapper: checking for duplicate `Type`") duplicates = m.DuplicateType() if duplicates != nil { - logger.Error("duplicate `Type` present in config") + logger.Error("Duplicate `Type` present in config") for _, i := range duplicates { logger.Errorf("'%v'", i) } err = fmt.Errorf("invalid") } - logger.Debug("mapper: checking for duplicate `Location`") + logger.Debug("Mapper: checking for duplicate `Location`") duplicates = m.DuplicateLocation() if duplicates != nil { - logger.Error("duplicate `Location` present in config") + logger.Error("Duplicate `Location` present in config") for _, i := range duplicates { logger.Errorf("'%v'", i) } From 43c7ffa3bf2eaf5c240442443532043ddf4bbc30 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 12:07:34 +1100 Subject: [PATCH 38/85] Moved file from helpers to it's own folder. --- cmd/diff.go | 6 ++--- cmd/validate_test.go | 38 ++++++++++++++--------------- cmd/visualize.go | 4 +-- cmd/visualize_test.go | 7 +++--- internal/tools/file/file.go | 12 +++++++++ internal/tools/file/file_test.go | 20 +++++++++++++++ internal/tools/helpers/file.go | 12 --------- internal/tools/helpers/file_test.go | 20 --------------- 8 files changed, 60 insertions(+), 59 deletions(-) create mode 100644 internal/tools/file/file.go create mode 100644 internal/tools/file/file_test.go delete mode 100644 internal/tools/helpers/file.go delete mode 100644 internal/tools/helpers/file_test.go diff --git a/cmd/diff.go b/cmd/diff.go index 39efaab..ce541a8 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -9,7 +9,7 @@ import ( "bytes" "fmt" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/microsoft/abstrakt/internal/tools/file" "github.com/microsoft/abstrakt/internal/tools/logger" "github.com/spf13/cobra" "strings" @@ -41,11 +41,11 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP logger.Debugf("showOriginalOutput: %t", *cc.showOriginal) logger.Debugf("showNewOutput: %t", *cc.showNew) - if !helpers.FileExists(cc.constellationFilePathOrg) { + if !file.Exists(cc.constellationFilePathOrg) { return fmt.Errorf("Could not open original YAML input file for reading %v", cc.constellationFilePathOrg) } - if !helpers.FileExists(cc.constellationFilePathNew) { + if !file.Exists(cc.constellationFilePathNew) { return fmt.Errorf("Could not open new YAML input file for reading %v", cc.constellationFilePathNew) } diff --git a/cmd/validate_test.go b/cmd/validate_test.go index a71d943..13fa4cb 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -36,7 +36,7 @@ func TestValidateCommandMapExist(t *testing.T) { } func TestValidateCommandConstellationFail(t *testing.T) { - expected := "constellation: open does-not-exist: no such file or directory" + expected := "Constellation: open does-not-exist: no such file or directory" hook := test.NewGlobal() _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", "does-not-exist") @@ -55,7 +55,7 @@ func TestValidateCommandConstellationFail(t *testing.T) { } func TestValidateCommandMapFail(t *testing.T) { - expected := "mapper: open does-not-exist: no such file or directory" + expected := "Mapper: open does-not-exist: no such file or directory" hook := test.NewGlobal() _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-m", "does-not-exist") @@ -78,8 +78,8 @@ func TestValidateCommandConstellationInvalidSchema(t *testing.T) { entries := helpers.GetAllLogs(hook.AllEntries()) assert.Error(t, err) - assert.Contains(t, entries, "constellation: invalid schema") - assert.EqualError(t, err, "invalid configuration(s)") + assert.Contains(t, entries, "Constellation: invalid schema") + assert.EqualError(t, err, "Invalid configuration(s)") } func TestValidateCommandNapperInvalidSchema(t *testing.T) { @@ -91,8 +91,8 @@ func TestValidateCommandNapperInvalidSchema(t *testing.T) { entries := helpers.GetAllLogs(hook.AllEntries()) assert.Error(t, err) - assert.Contains(t, entries, "mapper: invalid schema") - assert.EqualError(t, err, "invalid configuration(s)") + assert.Contains(t, entries, "Mapper: invalid schema") + assert.EqualError(t, err, "Invalid configuration(s)") } func TestValidateDeploymentFail(t *testing.T) { @@ -105,8 +105,8 @@ func TestValidateDeploymentFail(t *testing.T) { entries := helpers.GetAllLogs(hook.AllEntries()) assert.Error(t, err) - assert.Contains(t, entries, "service `EventLogger` does not exist in map") - assert.EqualError(t, err, "invalid configuration(s)") + assert.Contains(t, entries, "Service `EventLogger` does not exist in map") + assert.EqualError(t, err, "Invalid configuration(s)") } func TestValidateMapperDuplicates(t *testing.T) { @@ -118,11 +118,11 @@ func TestValidateMapperDuplicates(t *testing.T) { entries := helpers.GetAllLogs(hook.AllEntries()) assert.Error(t, err) - assert.Contains(t, entries, "duplicate `ChartName` present in config") - assert.Contains(t, entries, "duplicate `Type` present in config") - assert.Contains(t, entries, "duplicate `Location` present in config") - assert.Contains(t, entries, "mapper: invalid") - assert.EqualError(t, err, "invalid configuration(s)") + assert.Contains(t, entries, "Duplicate `ChartName` present in config") + assert.Contains(t, entries, "Duplicate `Type` present in config") + assert.Contains(t, entries, "Duplicate `Location` present in config") + assert.Contains(t, entries, "Mapper: invalid") + assert.EqualError(t, err, "Invalid configuration(s)") } func TestValidateConstellationDuplicateIDs(t *testing.T) { @@ -134,9 +134,9 @@ func TestValidateConstellationDuplicateIDs(t *testing.T) { entries := helpers.GetAllLogs(hook.AllEntries()) assert.Error(t, err) - assert.Contains(t, entries, "duplicate `ID` present in config") - assert.Contains(t, entries, "constellation: invalid") - assert.EqualError(t, err, "invalid configuration(s)") + assert.Contains(t, entries, "Duplicate `ID` present in config") + assert.Contains(t, entries, "Constellation: invalid") + assert.EqualError(t, err, "Invalid configuration(s)") } func TestValidateConstellationMissingServices(t *testing.T) { @@ -148,7 +148,7 @@ func TestValidateConstellationMissingServices(t *testing.T) { entries := helpers.GetAllLogs(hook.AllEntries()) assert.Error(t, err) - assert.Contains(t, entries, "relationship 'Event Hubs to Event Logger Link' has missing `Services`:") - assert.Contains(t, entries, "constellation: invalid") - assert.EqualError(t, err, "invalid configuration(s)") + assert.Contains(t, entries, "Relationship 'Event Hubs to Event Logger Link' has missing `Services`:") + assert.Contains(t, entries, "Constellation: invalid") + assert.EqualError(t, err, "Invalid configuration(s)") } diff --git a/cmd/visualize.go b/cmd/visualize.go index 4110280..bd1ae85 100644 --- a/cmd/visualize.go +++ b/cmd/visualize.go @@ -9,7 +9,7 @@ import ( "bytes" "fmt" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/microsoft/abstrakt/internal/tools/file" "github.com/microsoft/abstrakt/internal/tools/logger" "github.com/spf13/cobra" "strings" @@ -34,7 +34,7 @@ Example: abstrakt visualize -f [constellationFilePath]`, logger.Debug("args: " + strings.Join(args, " ")) logger.Debug("constellationFilePath: " + cc.constellationFilePath) - if !helpers.FileExists(cc.constellationFilePath) { + if !file.Exists(cc.constellationFilePath) { return fmt.Errorf("Could not open YAML input file for reading %v", cc.constellationFilePath) } diff --git a/cmd/visualize_test.go b/cmd/visualize_test.go index b3064bd..81c6ed4 100644 --- a/cmd/visualize_test.go +++ b/cmd/visualize_test.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/microsoft/abstrakt/internal/platform/constellation" + "github.com/microsoft/abstrakt/internal/tools/file" "github.com/microsoft/abstrakt/internal/tools/helpers" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" @@ -63,11 +64,11 @@ func TestFileExists(t *testing.T) { assert.NoError(t, err, "Could not create output testing file, cannot proceed") //Test that a valid file (created above) can be seen - var result bool = helpers.FileExists(testValidFilename) //Expecting true - file does exists + var result bool = file.Exists(testValidFilename) //Expecting true - file does exists assert.True(t, result, "Test file does exist but testFile returns that it does not") //Test that an invalid file (does not exist) is not seen - result = helpers.FileExists(testInvalidFilename) //Expecting false - file does not exist + result = file.Exists(testInvalidFilename) //Expecting false - file does not exist assert.False(t, result, "Test file does not exist but testFile says it does") err = os.Remove(testValidFilename) @@ -75,7 +76,7 @@ func TestFileExists(t *testing.T) { panic(err) } - result = helpers.FileExists(testValidFilename) //Expecting false - file has been removed + result = file.Exists(testValidFilename) //Expecting false - file has been removed assert.False(t, result, "Test file has been removed but fileExists is finding it") } diff --git a/internal/tools/file/file.go b/internal/tools/file/file.go new file mode 100644 index 0000000..bc12bc6 --- /dev/null +++ b/internal/tools/file/file.go @@ -0,0 +1,12 @@ +package file + +import "os" + +// Exists - basic utility function to check the provided filename can be opened and is not a folder/directory +func Exists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} diff --git a/internal/tools/file/file_test.go b/internal/tools/file/file_test.go new file mode 100644 index 0000000..edbdb5b --- /dev/null +++ b/internal/tools/file/file_test.go @@ -0,0 +1,20 @@ +package file_test + +import ( + "github.com/microsoft/abstrakt/internal/tools/file" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestFileExistsNoFile(t *testing.T) { + assert.False(t, file.Exists("does-not-exist")) +} + +func TestFileExists(t *testing.T) { + assert.True(t, file.Exists("../../../README.md")) +} + +func TestFileExistsIsFolder(t *testing.T) { + assert.False(t, file.Exists("../file")) + assert.False(t, file.Exists("../file/")) +} diff --git a/internal/tools/helpers/file.go b/internal/tools/helpers/file.go deleted file mode 100644 index da31129..0000000 --- a/internal/tools/helpers/file.go +++ /dev/null @@ -1,12 +0,0 @@ -package helpers - -import "os" - -// FileExists - basic utility function to check the provided filename can be opened and is not a folder/directory -func FileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() -} diff --git a/internal/tools/helpers/file_test.go b/internal/tools/helpers/file_test.go deleted file mode 100644 index 22c832b..0000000 --- a/internal/tools/helpers/file_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package helpers_test - -import ( - "github.com/microsoft/abstrakt/internal/tools/helpers" - "github.com/stretchr/testify/assert" - "testing" -) - -func TestFileExistsNoFile(t *testing.T) { - assert.False(t, helpers.FileExists("does-not-exist")) -} - -func TestFileExists(t *testing.T) { - assert.True(t, helpers.FileExists("../../../README.md")) -} - -func TestFileExistsFolder(t *testing.T) { - assert.False(t, helpers.FileExists("../helpers")) - assert.False(t, helpers.FileExists("../helpers/")) -} From f5fe20a74b58ec70be3ea5ce9d61ad4562de6469 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 13:51:13 +1100 Subject: [PATCH 39/85] Moved find from helpers to tool. --- cmd/validate.go | 6 +++--- internal/platform/constellation/validate.go | 10 +++++----- internal/platform/mapper/validate.go | 8 ++++---- internal/tools/{helpers => find}/find.go | 6 +++--- internal/tools/{helpers => find}/find_test.go | 8 ++++---- 5 files changed, 19 insertions(+), 19 deletions(-) rename internal/tools/{helpers => find}/find.go (54%) rename internal/tools/{helpers => find}/find_test.go (70%) diff --git a/cmd/validate.go b/cmd/validate.go index c3ed9fa..16902a9 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/platform/mapper" - "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/microsoft/abstrakt/internal/tools/find" "github.com/microsoft/abstrakt/internal/tools/logger" "github.com/spf13/cobra" ) @@ -89,7 +89,7 @@ func validateDagAndMapper(d *constellation.Config, m *mapper.Config) (err error) mapTypes := []string{} for _, i := range d.Services { - _, exists := helpers.Find(types, i.Type) + _, exists := find.Slice(types, i.Type) if !exists { types = append(types, i.Type) } @@ -101,7 +101,7 @@ func validateDagAndMapper(d *constellation.Config, m *mapper.Config) (err error) logger.Debug("deployment: checking if `Service` exists in map") for _, i := range types { - _, exists := helpers.Find(mapTypes, i) + _, exists := find.Slice(mapTypes, i) if !exists { logger.Error("Missing map configuration(s)") logger.Errorf("Service `%v` does not exist in map", i) diff --git a/internal/platform/constellation/validate.go b/internal/platform/constellation/validate.go index 60aadf7..dceda3b 100644 --- a/internal/platform/constellation/validate.go +++ b/internal/platform/constellation/validate.go @@ -1,7 +1,7 @@ package constellation import ( - "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/microsoft/abstrakt/internal/tools/find" "gopkg.in/dealancer/validate.v2" ) @@ -10,7 +10,7 @@ func (m *Config) DuplicateIDs() (duplicates []string) { IDs := []string{string(m.ID)} for _, i := range m.Services { - _, exists := helpers.Find(IDs, i.ID) + _, exists := find.Slice(IDs, i.ID) if exists { duplicates = append(duplicates, i.ID) } else { @@ -19,7 +19,7 @@ func (m *Config) DuplicateIDs() (duplicates []string) { } for _, i := range m.Relationships { - _, exists := helpers.Find(IDs, i.ID) + _, exists := find.Slice(IDs, i.ID) if exists { duplicates = append(duplicates, i.ID) } else { @@ -40,12 +40,12 @@ func (m *Config) CheckServiceExists() (missing map[string][]string) { } for _, i := range m.Relationships { - _, exists := helpers.Find(IDs, i.To) + _, exists := find.Slice(IDs, i.To) if !exists { missing[i.ID] = append(missing[i.ID], i.To) } - _, exists = helpers.Find(IDs, i.From) + _, exists = find.Slice(IDs, i.From) if !exists { missing[i.ID] = append(missing[i.ID], i.From) } diff --git a/internal/platform/mapper/validate.go b/internal/platform/mapper/validate.go index 42f377f..3474719 100644 --- a/internal/platform/mapper/validate.go +++ b/internal/platform/mapper/validate.go @@ -1,7 +1,7 @@ package mapper import ( - "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/microsoft/abstrakt/internal/tools/find" "gopkg.in/dealancer/validate.v2" ) @@ -10,7 +10,7 @@ func (m *Config) DuplicateChartName() (duplicates []string) { chartNames := []string{} for _, i := range m.Maps { - _, exists := helpers.Find(chartNames, i.ChartName) + _, exists := find.Slice(chartNames, i.ChartName) if exists { duplicates = append(duplicates, i.ChartName) } else { @@ -26,7 +26,7 @@ func (m *Config) DuplicateType() (duplicates []string) { types := []string{} for _, i := range m.Maps { - _, exists := helpers.Find(types, i.Type) + _, exists := find.Slice(types, i.Type) if exists { duplicates = append(duplicates, i.Type) } else { @@ -42,7 +42,7 @@ func (m *Config) DuplicateLocation() (duplicates []string) { location := []string{} for _, i := range m.Maps { - _, exists := helpers.Find(location, i.Location) + _, exists := find.Slice(location, i.Location) if exists { duplicates = append(duplicates, i.Location) } else { diff --git a/internal/tools/helpers/find.go b/internal/tools/find/find.go similarity index 54% rename from internal/tools/helpers/find.go rename to internal/tools/find/find.go index 669eddb..e098ccf 100644 --- a/internal/tools/helpers/find.go +++ b/internal/tools/find/find.go @@ -1,8 +1,8 @@ -package helpers +package find -// Find takes a slice and looks for an element in it. If found it will +// Slice takes a slice and looks for an element in it. If found it will // return it's key, otherwise it will return -1 and a bool of false. -func Find(slice []string, val string) (int, bool) { +func Slice(slice []string, val string) (int, bool) { for i, item := range slice { if item == val { return i, true diff --git a/internal/tools/helpers/find_test.go b/internal/tools/find/find_test.go similarity index 70% rename from internal/tools/helpers/find_test.go rename to internal/tools/find/find_test.go index 7b033d1..b069f52 100644 --- a/internal/tools/helpers/find_test.go +++ b/internal/tools/find/find_test.go @@ -1,7 +1,7 @@ -package helpers_test +package find_test import ( - "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/microsoft/abstrakt/internal/tools/find" "github.com/stretchr/testify/assert" "testing" ) @@ -9,7 +9,7 @@ import ( func TestFindElementInSlice(t *testing.T) { slice := []string{"a", "b", "c", "d", "e", "f"} - index, found := helpers.Find(slice, "d") + index, found := find.Slice(slice, "d") assert.True(t, found) assert.NotEqual(t, -1, index) @@ -19,7 +19,7 @@ func TestFindElementInSlice(t *testing.T) { func TestFindElementNotInSlice(t *testing.T) { slice := []string{"a", "b", "c", "d", "e", "f"} - index, found := helpers.Find(slice, "g") + index, found := find.Slice(slice, "g") assert.False(t, found) assert.Equal(t, -1, index) From fff8153d01ec995d95a775c4e8b4c6f88723e28a Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 14:06:03 +1100 Subject: [PATCH 40/85] Moved tests from helpers to its own folder. --- cmd/compose_test.go | 32 +++++++-------- cmd/diff_test.go | 16 ++++---- cmd/validate_test.go | 40 +++++++++---------- cmd/version_test.go | 4 +- cmd/visualize_test.go | 14 +++---- internal/platform/constellation/diff_test.go | 4 +- internal/platform/constellation/graph_test.go | 4 +- internal/tools/{helpers => test}/test.go | 2 +- 8 files changed, 58 insertions(+), 58 deletions(-) rename internal/tools/{helpers => test}/test.go (99%) diff --git a/cmd/compose_test.go b/cmd/compose_test.go index bc72a66..c340e95 100644 --- a/cmd/compose_test.go +++ b/cmd/compose_test.go @@ -1,45 +1,45 @@ package cmd import ( - "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/microsoft/abstrakt/internal/tools/test" "github.com/stretchr/testify/assert" "testing" ) func TestComposeCommandReturnsErrorIfTemplateTypeIsInvalid(t *testing.T) { templateType := "ble" - constellationPath, mapsPath, tdir := helpers.PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := test.PrepareRealFilesForTest(t) - defer helpers.CleanTempTestFiles(t, tdir) + defer test.CleanTempTestFiles(t, tdir) - output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-if-template-type-is-invalid", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-if-template-type-is-invalid", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) assert.Errorf(t, err, "Did not received expected error. \nGot:\n %v", output) } func TestComposeCommandDoesNotErrorIfTemplateTypeIsEmptyOrHelm(t *testing.T) { templateType := "" - constellationPath, mapsPath, tdir := helpers.PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := test.PrepareRealFilesForTest(t) - defer helpers.CleanTempTestFiles(t, tdir) + defer test.CleanTempTestFiles(t, tdir) - output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) assert.NoErrorf(t, err, "Did not expect error:\n %v\n output: %v", err, output) templateType = "helm" - output, err = helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + output, err = test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) assert.NoErrorf(t, err, "Did not expect error:\n %v\n output: %v", err, output) } func TestComposeCommandReturnsErrorWithInvalidFilePaths(t *testing.T) { - output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-with-invalid-files", "-f", "invalid", "-m", "invalid", "-o", "invalid") + output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-with-invalid-files", "-f", "invalid", "-m", "invalid", "-o", "invalid") assert.Errorf(t, err, "Did not received expected error. \nGot:\n %v", output) } func TestComposeCmdVerifyRequiredFlags(t *testing.T) { expected := "required flag(s) \"constellationFilePath\", \"mapsFilePath\", \"outputPath\" not set" - output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "") + output, err := test.ExecuteCommand(newComposeCmd().cmd, "") if err != nil { assert.Contains(t, err.Error(), expected) @@ -49,19 +49,19 @@ func TestComposeCmdVerifyRequiredFlags(t *testing.T) { } func TestComposeCmdWithValidFlags(t *testing.T) { - constellationPath, mapsPath, tdir := helpers.PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := test.PrepareRealFilesForTest(t) - defer helpers.CleanTempTestFiles(t, tdir) + defer test.CleanTempTestFiles(t, tdir) - output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-flags", "-f", constellationPath, "-m", mapsPath, "-o", tdir) + output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-flags", "-f", constellationPath, "-m", mapsPath, "-o", tdir) assert.NoErrorf(t, err, "error: \n %v\noutput:\n %v\n", err, output) } func TestComposeWithRealFiles(t *testing.T) { - constellationPath, mapsPath, tdir := helpers.PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := test.PrepareRealFilesForTest(t) - defer helpers.CleanTempTestFiles(t, tdir) + defer test.CleanTempTestFiles(t, tdir) - output, err := helpers.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-real-files", "-f", constellationPath, "-m", mapsPath, "-o", tdir) + output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-real-files", "-f", constellationPath, "-m", mapsPath, "-o", tdir) assert.NoErrorf(t, err, "error: \n %v\noutput:\n %v\n", err, output) } diff --git a/cmd/diff_test.go b/cmd/diff_test.go index 02ed656..71f06d1 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/microsoft/abstrakt/internal/tools/helpers" + helper "github.com/microsoft/abstrakt/internal/tools/test" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" "testing" @@ -10,15 +10,15 @@ import ( // TestDiffCmdWithAllRequirementsNoError - test diff command parameters // use valid arguments so expect no failures func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { - constellationPathOrg, constellationPathNew, _, _ := helpers.PrepareTwoRealConstellationFilesForTest(t) + constellationPathOrg, constellationPathNew, _, _ := helper.PrepareTwoRealConstellationFilesForTest(t) hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", constellationPathOrg, "-n", constellationPathNew) + _, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", constellationPathOrg, "-n", constellationPathNew) if err != nil { t.Error("Did not receive output") } else { - assert.Truef(t, helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, hook.LastEntry().Message), "Expcted output and produced output do not match : expected %s produced %s", testDiffComparisonOutputString, hook.LastEntry().Message) + assert.Truef(t, helper.CompareGraphOutputAsSets(testDiffComparisonOutputString, hook.LastEntry().Message), "Expcted output and produced output do not match : expected %s produced %s", testDiffComparisonOutputString, hook.LastEntry().Message) // Did use this initially but wont work with the strongs output from the graphviz library as the sequence of entries in the output can change // while the sequence may change the result is still valid and the same so am usinga local comparison function to get around this problem // assert.Contains(t, hook.LastEntry().Message, testDiffComparisonOutputString) @@ -30,7 +30,7 @@ func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { func TestDffCmdFailYaml(t *testing.T) { expected := "Could not open original YAML input file for reading constellationPathOrg" - output, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "constellationPathOrg", "-n", "constellationPathNew") + output, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", "constellationPathOrg", "-n", "constellationPathNew") if err != nil { assert.Contains(t, err.Error(), expected) @@ -40,7 +40,7 @@ func TestDffCmdFailYaml(t *testing.T) { expected = "Could not open new YAML input file for reading constellationPathNew" - output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "constellationPathNew") + output, err = helper.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "constellationPathNew") if err != nil { assert.Contains(t, err.Error(), expected) @@ -55,7 +55,7 @@ func TestDffCmdFailYaml(t *testing.T) { func TestDiffCmdFailNotYaml(t *testing.T) { expected := "dagConfigService failed to load file" - output, err := helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "diff.go", "-n", "diff.go") + output, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", "diff.go", "-n", "diff.go") if err != nil { assert.Contains(t, err.Error(), expected) @@ -63,7 +63,7 @@ func TestDiffCmdFailNotYaml(t *testing.T) { t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) } - output, err = helpers.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "diff.go") + output, err = helper.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "diff.go") if err != nil { assert.Contains(t, err.Error(), expected) diff --git a/cmd/validate_test.go b/cmd/validate_test.go index 13fa4cb..b3a3145 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -1,14 +1,14 @@ package cmd import ( - "github.com/microsoft/abstrakt/internal/tools/helpers" + helper "github.com/microsoft/abstrakt/internal/tools/test" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" "testing" ) func TestValidateCommandNoArgs(t *testing.T) { - _, err := helpers.ExecuteCommand(newValidateCmd().cmd) + _, err := helper.ExecuteCommand(newValidateCmd().cmd) assert.Error(t, err) assert.EqualError(t, err, "no flags were set") } @@ -17,21 +17,21 @@ func TestValidateCommandConstellationAndMapper(t *testing.T) { constellationPath := "testdata/constellation/valid.yaml" mapPath := "testdata/mapper/valid.yaml" - output, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath, "-m", mapPath) + output, err := helper.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath, "-m", mapPath) assert.NoErrorf(t, err, "Did not received expected error. \nGot:\n %v", output) } func TestValidateCommandConstellationExist(t *testing.T) { constellationPath := "testdata/constellation/valid.yaml" - output, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) + output, err := helper.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) assert.NoErrorf(t, err, "Did not received expected error. \nGot:\n %v", output) } func TestValidateCommandMapExist(t *testing.T) { mapPath := "testdata/mapper/valid.yaml" - output, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-m", mapPath) + output, err := helper.ExecuteCommand(newValidateCmd().cmd, "-m", mapPath) assert.NoErrorf(t, err, "Did not received expected error. \nGot:\n %v", output) } @@ -39,7 +39,7 @@ func TestValidateCommandConstellationFail(t *testing.T) { expected := "Constellation: open does-not-exist: no such file or directory" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", "does-not-exist") + _, err := helper.ExecuteCommand(newValidateCmd().cmd, "-f", "does-not-exist") entries := []string{} @@ -58,9 +58,9 @@ func TestValidateCommandMapFail(t *testing.T) { expected := "Mapper: open does-not-exist: no such file or directory" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-m", "does-not-exist") + _, err := helper.ExecuteCommand(newValidateCmd().cmd, "-m", "does-not-exist") - entries := helpers.GetAllLogs(hook.AllEntries()) + entries := helper.GetAllLogs(hook.AllEntries()) if err != nil { assert.Contains(t, entries, expected) @@ -73,9 +73,9 @@ func TestValidateCommandConstellationInvalidSchema(t *testing.T) { mapPath := "testdata/mapper/valid.yaml" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", mapPath) + _, err := helper.ExecuteCommand(newValidateCmd().cmd, "-f", mapPath) - entries := helpers.GetAllLogs(hook.AllEntries()) + entries := helper.GetAllLogs(hook.AllEntries()) assert.Error(t, err) assert.Contains(t, entries, "Constellation: invalid schema") @@ -86,9 +86,9 @@ func TestValidateCommandNapperInvalidSchema(t *testing.T) { constellationPath := "testdata/constellation/valid.yaml" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-m", constellationPath) + _, err := helper.ExecuteCommand(newValidateCmd().cmd, "-m", constellationPath) - entries := helpers.GetAllLogs(hook.AllEntries()) + entries := helper.GetAllLogs(hook.AllEntries()) assert.Error(t, err) assert.Contains(t, entries, "Mapper: invalid schema") @@ -100,9 +100,9 @@ func TestValidateDeploymentFail(t *testing.T) { mapPath := "testdata/mapper/invalid.yaml" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath, "-m", mapPath) + _, err := helper.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath, "-m", mapPath) - entries := helpers.GetAllLogs(hook.AllEntries()) + entries := helper.GetAllLogs(hook.AllEntries()) assert.Error(t, err) assert.Contains(t, entries, "Service `EventLogger` does not exist in map") @@ -113,9 +113,9 @@ func TestValidateMapperDuplicates(t *testing.T) { mapPath := "testdata/mapper/invalid.yaml" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-m", mapPath) + _, err := helper.ExecuteCommand(newValidateCmd().cmd, "-m", mapPath) - entries := helpers.GetAllLogs(hook.AllEntries()) + entries := helper.GetAllLogs(hook.AllEntries()) assert.Error(t, err) assert.Contains(t, entries, "Duplicate `ChartName` present in config") @@ -129,9 +129,9 @@ func TestValidateConstellationDuplicateIDs(t *testing.T) { constellationPath := "testdata/constellation/invalid.yaml" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) + _, err := helper.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) - entries := helpers.GetAllLogs(hook.AllEntries()) + entries := helper.GetAllLogs(hook.AllEntries()) assert.Error(t, err) assert.Contains(t, entries, "Duplicate `ID` present in config") @@ -143,9 +143,9 @@ func TestValidateConstellationMissingServices(t *testing.T) { constellationPath := "testdata/constellation/invalid.yaml" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) + _, err := helper.ExecuteCommand(newValidateCmd().cmd, "-f", constellationPath) - entries := helpers.GetAllLogs(hook.AllEntries()) + entries := helper.GetAllLogs(hook.AllEntries()) assert.Error(t, err) assert.Contains(t, entries, "Relationship 'Event Hubs to Event Logger Link' has missing `Services`:") diff --git a/cmd/version_test.go b/cmd/version_test.go index 3d8a3df..63e26ee 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/microsoft/abstrakt/internal/tools/helpers" + helper "github.com/microsoft/abstrakt/internal/tools/test" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" "os" @@ -24,7 +24,7 @@ func TestVersionCmd(t *testing.T) { expected := "0.0.1" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newVersionCmd().cmd) + _, err := helper.ExecuteCommand(newVersionCmd().cmd) if err != nil { t.Error(err) diff --git a/cmd/visualize_test.go b/cmd/visualize_test.go index 81c6ed4..c1c0384 100644 --- a/cmd/visualize_test.go +++ b/cmd/visualize_test.go @@ -3,7 +3,7 @@ package cmd import ( "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/file" - "github.com/microsoft/abstrakt/internal/tools/helpers" + helper "github.com/microsoft/abstrakt/internal/tools/test" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" "io/ioutil" @@ -16,19 +16,19 @@ func TestVisualizeCmdWithAllRequirementsNoError(t *testing.T) { constellationPath := "testdata/constellation/valid.yaml" hook := test.NewGlobal() - _, err := helpers.ExecuteCommand(newVisualizeCmd().cmd, "-f", constellationPath) + _, err := helper.ExecuteCommand(newVisualizeCmd().cmd, "-f", constellationPath) if err != nil { t.Error("Did not receive output") } else { - assert.Truef(t, helpers.CompareGraphOutputAsSets(validGraphString, hook.LastEntry().Message), "Expcted output and produced output do not match : expected %s produced %s", validGraphString, hook.LastEntry().Message) + assert.Truef(t, helper.CompareGraphOutputAsSets(validGraphString, hook.LastEntry().Message), "Expcted output and produced output do not match : expected %s produced %s", validGraphString, hook.LastEntry().Message) } } func TestVisualizeCmdFailYaml(t *testing.T) { expected := "Could not open YAML input file for reading" - output, err := helpers.ExecuteCommand(newVisualizeCmd().cmd, "-f", "constellationPath") + output, err := helper.ExecuteCommand(newVisualizeCmd().cmd, "-f", "constellationPath") if err != nil { assert.Contains(t, err.Error(), expected) @@ -40,7 +40,7 @@ func TestVisualizeCmdFailYaml(t *testing.T) { func TestVisualizeCmdFailNotYaml(t *testing.T) { expected := "dagConfigService failed to load file" - output, err := helpers.ExecuteCommand(newVisualizeCmd().cmd, "-f", "visualize.go") + output, err := helper.ExecuteCommand(newVisualizeCmd().cmd, "-f", "visualize.go") if err != nil { assert.Contains(t, err.Error(), expected) @@ -50,9 +50,9 @@ func TestVisualizeCmdFailNotYaml(t *testing.T) { } func TestFileExists(t *testing.T) { - _, _, tdir := helpers.PrepareRealFilesForTest(t) + _, _, tdir := helper.PrepareRealFilesForTest(t) - defer helpers.CleanTempTestFiles(t, tdir) + defer helper.CleanTempTestFiles(t, tdir) //Setup variables and content for test testValidFilename := filepath.Join(tdir, "testVisualize.out") diff --git a/internal/platform/constellation/diff_test.go b/internal/platform/constellation/diff_test.go index 1822473..6f7c188 100644 --- a/internal/platform/constellation/diff_test.go +++ b/internal/platform/constellation/diff_test.go @@ -3,7 +3,7 @@ package constellation_test import ( set "github.com/deckarep/golang-set" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/microsoft/abstrakt/internal/tools/test" "github.com/stretchr/testify/assert" "testing" ) @@ -55,7 +55,7 @@ func TestGraphWithChanges(t *testing.T) { resString, err := constellation.CreateGraphWithChanges(dsGraphNew, &loadedSets) assert.NoError(t, err) - assert.Truef(t, helpers.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString), "Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) + assert.Truef(t, test.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString), "Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) } // Utility to populate comparison sets with expected/known result diff --git a/internal/platform/constellation/graph_test.go b/internal/platform/constellation/graph_test.go index d39263d..c9180d7 100644 --- a/internal/platform/constellation/graph_test.go +++ b/internal/platform/constellation/graph_test.go @@ -3,7 +3,7 @@ package constellation_test import ( "bytes" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/helpers" + "github.com/microsoft/abstrakt/internal/tools/test" "github.com/stretchr/testify/assert" "testing" ) @@ -25,7 +25,7 @@ func TestGenerateGraph(t *testing.T) { retString, err := retConfig.GenerateGraph(out) assert.NoErrorf(t, err, "Should not receive error: %v", err) - assert.True(t, helpers.CompareGraphOutputAsSets(cmpString, retString), "Input graph did not generate expected output graphviz representation\nExpected:\n%v \nGot:\n%v", cmpString, retString) + assert.True(t, test.CompareGraphOutputAsSets(cmpString, retString), "Input graph did not generate expected output graphviz representation\nExpected:\n%v \nGot:\n%v", cmpString, retString) } const test02ConstGraphString = `digraph Azure_Event_Hubs_Sample { diff --git a/internal/tools/helpers/test.go b/internal/tools/test/test.go similarity index 99% rename from internal/tools/helpers/test.go rename to internal/tools/test/test.go index 92a9637..932abbc 100644 --- a/internal/tools/helpers/test.go +++ b/internal/tools/test/test.go @@ -1,4 +1,4 @@ -package helpers +package test import ( "bytes" From 61a9a684e66a640d70fc0fa730676d4f013e4d3a Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 14:50:53 +1100 Subject: [PATCH 41/85] Moved diff from constellation to its own folder. --- cmd/diff.go | 4 ++- .../{platform/constellation => diff}/diff.go | 25 ++++++++++++------- .../constellation => diff}/diff_test.go | 23 +++++++++-------- .../diff => diff/testdata}/modified.yaml | 0 .../diff => diff/testdata}/original.yaml | 0 5 files changed, 32 insertions(+), 20 deletions(-) rename internal/{platform/constellation => diff}/diff.go (91%) rename internal/{platform/constellation => diff}/diff_test.go (88%) rename internal/{platform/constellation/testdata/diff => diff/testdata}/modified.yaml (100%) rename internal/{platform/constellation/testdata/diff => diff/testdata}/original.yaml (100%) diff --git a/cmd/diff.go b/cmd/diff.go index ce541a8..1e95d39 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -8,6 +8,7 @@ package cmd import ( "bytes" "fmt" + "github.com/microsoft/abstrakt/internal/diff" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/file" "github.com/microsoft/abstrakt/internal/tools/logger" @@ -81,7 +82,8 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP logger.Output(resStringNew) } - resStringDiff, err := constellation.CompareConstellations(dsGraphOrg, dsGraphNew) + constellationSets := diff.Set{Original: dsGraphOrg, New: dsGraphNew} + resStringDiff, err := constellationSets.CompareConstellations() if err != nil { return err diff --git a/internal/platform/constellation/diff.go b/internal/diff/diff.go similarity index 91% rename from internal/platform/constellation/diff.go rename to internal/diff/diff.go index ac5a230..239da88 100644 --- a/internal/platform/constellation/diff.go +++ b/internal/diff/diff.go @@ -1,9 +1,10 @@ -package constellation +package diff import ( "fmt" "github.com/awalterschulze/gographviz" set "github.com/deckarep/golang-set" + "github.com/microsoft/abstrakt/internal/platform/constellation" "strings" ) @@ -17,20 +18,26 @@ type ComparisonSet struct { SetDelRels set.Set } +// Set contains two constellation configurations to compare against one another. +type Set struct { + Original *constellation.Config + New *constellation.Config +} + //CompareConstellations takes two constellation configurations, compares and returns the differences. -func CompareConstellations(dsOrg *Config, dsNew *Config) (string, error) { +func (d *Set) CompareConstellations() (string, error) { // populate comparison sets with changes between original and new graph - sets := FillComparisonSets(dsOrg, dsNew) + sets := d.FillComparisonSets() // build the graphviz output from new graph and comparison sets - return CreateGraphWithChanges(dsNew, &sets) + return CreateGraphWithChanges(d.New, &sets) } // FillComparisonSets - loads provided set struct with data from the constellations and then determines the various differences between the // sets (original constellation and new) to help detemine what has been added, removed or changed. -func FillComparisonSets(dsOrg *Config, dsNew *Config) (sets ComparisonSet) { - setOrgSvcs, setOrgRel := createSet(dsOrg) - setNewSvcs, setNewRel := createSet(dsNew) +func (d *Set) FillComparisonSets() (sets ComparisonSet) { + setOrgSvcs, setOrgRel := createSet(d.Original) + setNewSvcs, setNewRel := createSet(d.New) // "Added" means in the new constellation but not the original one // "Deleted" means something is present in the original constellation but not in the new constellation @@ -47,7 +54,7 @@ func FillComparisonSets(dsOrg *Config, dsNew *Config) (sets ComparisonSet) { } // createSet - utility function used to create a pair of result sets (services + relationships) based on an input constellation DAG -func createSet(dsGraph *Config) (set.Set, set.Set) { +func createSet(dsGraph *constellation.Config) (set.Set, set.Set) { // Create sets to hold services and relationships - used to find differences between old and new using intersection and difference operations retSetServices := set.NewSet() @@ -69,7 +76,7 @@ func createSet(dsGraph *Config) (set.Set, set.Set) { // CreateGraphWithChanges - use both input constellations (new and original) as well as the comparison sets to create // a dag that can be visualized. It uses the comparison sets to identify additions, deletions and changes between the original // and new constellations. -func CreateGraphWithChanges(newGraph *Config, sets *ComparisonSet) (string, error) { +func CreateGraphWithChanges(newGraph *constellation.Config, sets *ComparisonSet) (string, error) { // Lookup is used to map IDs to names. Names are easier to visualize but IDs are more important to ensure the // presented constellation is correct and IDs are used to link nodes together lookup := make(map[string]string) diff --git a/internal/platform/constellation/diff_test.go b/internal/diff/diff_test.go similarity index 88% rename from internal/platform/constellation/diff_test.go rename to internal/diff/diff_test.go index 6f7c188..83dba77 100644 --- a/internal/platform/constellation/diff_test.go +++ b/internal/diff/diff_test.go @@ -1,7 +1,8 @@ -package constellation_test +package diff_test import ( set "github.com/deckarep/golang-set" + "github.com/microsoft/abstrakt/internal/diff" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/tools/test" "github.com/stretchr/testify/assert" @@ -13,20 +14,21 @@ import ( func TestGetComparisonSets(t *testing.T) { dsGraphOrg := new(constellation.Config) - err := dsGraphOrg.LoadFile("testdata/diff/original.yaml") + err := dsGraphOrg.LoadFile("testdata/original.yaml") assert.NoErrorf(t, err, "dagConfigService failed to load dag from test string %s", err) dsGraphNew := new(constellation.Config) - err = dsGraphNew.LoadFile("testdata/diff/modified.yaml") + err = dsGraphNew.LoadFile("testdata/modified.yaml") assert.NoErrorf(t, err, "dagConfigService failed to load file %s", err) // will populate with expected/known outcomes - knownSets := &constellation.ComparisonSet{} + knownSets := &diff.ComparisonSet{} populateComparisonSets(knownSets) // function being tested - loadedSets := constellation.FillComparisonSets(dsGraphOrg, dsGraphNew) + diffSet := diff.Set{Original: dsGraphOrg, New: dsGraphNew} + loadedSets := diffSet.FillComparisonSets() assert.True(t, knownSets.SetCommonSvcs.Equal(loadedSets.SetCommonSvcs), "Common services - did not match between expected result and input yaml") assert.True(t, knownSets.SetCommonRels.Equal(loadedSets.SetCommonRels), "Common relationships - did not match between expected result and input yaml") @@ -40,26 +42,27 @@ func TestGetComparisonSets(t *testing.T) { func TestGraphWithChanges(t *testing.T) { dsGraphOrg := new(constellation.Config) - err := dsGraphOrg.LoadFile("testdata/diff/original.yaml") + err := dsGraphOrg.LoadFile("testdata/original.yaml") if err != nil { t.Errorf("dagConfigService failed to load dag from test string %s", err) } dsGraphNew := new(constellation.Config) - err = dsGraphNew.LoadFile("testdata/diff/modified.yaml") + err = dsGraphNew.LoadFile("testdata/modified.yaml") assert.NoErrorf(t, err, "dagConfigService failed to load file %s", err) // function being tested - loadedSets := constellation.FillComparisonSets(dsGraphOrg, dsGraphNew) + diffSet := diff.Set{Original: dsGraphOrg, New: dsGraphNew} + loadedSets := diffSet.FillComparisonSets() - resString, err := constellation.CreateGraphWithChanges(dsGraphNew, &loadedSets) + resString, err := diff.CreateGraphWithChanges(dsGraphNew, &loadedSets) assert.NoError(t, err) assert.Truef(t, test.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString), "Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) } // Utility to populate comparison sets with expected/known result -func populateComparisonSets(target *constellation.ComparisonSet) { +func populateComparisonSets(target *diff.ComparisonSet) { target.SetCommonSvcs = set.NewSet() target.SetCommonSvcs.Add("3aa1e546-1ed5-4d67-a59c-be0d5905b490") target.SetCommonSvcs.Add("1d0255d4-5b8c-4a52-b0bb-ac024cda37e5") diff --git a/internal/platform/constellation/testdata/diff/modified.yaml b/internal/diff/testdata/modified.yaml similarity index 100% rename from internal/platform/constellation/testdata/diff/modified.yaml rename to internal/diff/testdata/modified.yaml diff --git a/internal/platform/constellation/testdata/diff/original.yaml b/internal/diff/testdata/original.yaml similarity index 100% rename from internal/platform/constellation/testdata/diff/original.yaml rename to internal/diff/testdata/original.yaml From e8552b47bf184485e537f086cb14bdd2aaddf0f9 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 14:52:15 +1100 Subject: [PATCH 42/85] Remove get_helm.sh after installing. --- makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/makefile b/makefile index 7b9f11c..41919d2 100644 --- a/makefile +++ b/makefile @@ -59,6 +59,7 @@ install-helm: curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 > get_helm.sh chmod 700 get_helm.sh ./get_helm.sh + rm get_helm.sh create-kindcluster: ifeq (,$(shell kind get clusters)) From 0e2fbbce808b3b9f1167a3d9beb1e33eb44d06c1 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 15:05:18 +1100 Subject: [PATCH 43/85] Cleanup compose. --- internal/compose/compose.go | 31 +++------ internal/compose/compose_test.go | 72 +++----------------- internal/compose/testdata/constellation.yaml | 38 +++++++++++ internal/compose/testdata/mapper.yaml | 15 ++++ 4 files changed, 74 insertions(+), 82 deletions(-) create mode 100644 internal/compose/testdata/constellation.yaml create mode 100644 internal/compose/testdata/mapper.yaml diff --git a/internal/compose/compose.go b/internal/compose/compose.go index a6ef117..cb208f7 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -15,8 +15,8 @@ type Composer struct { } //Build takes the loaded DAG and maps and builds the Helm values and requirements documents -func (m *Composer) Build(name string, dir string) (*helm.Chart, error) { - if m.Constellation.Name == "" || m.Mapper.Name == "" { +func (c *Composer) Build(name string, dir string) (*helm.Chart, error) { + if c.Constellation.Name == "" || c.Mapper.Name == "" { return nil, fmt.Errorf("Please initialize with LoadFromFile or LoadFromString") } @@ -32,8 +32,8 @@ func (m *Composer) Build(name string, dir string) (*helm.Chart, error) { values := newChart.Values - for _, n := range m.Constellation.Services { - service := m.Mapper.FindByType(n.Type) + for _, n := range c.Constellation.Services { + service := c.Mapper.FindByType(n.Type) if service == nil { return nil, fmt.Errorf("Could not find service %v", service) } @@ -66,15 +66,15 @@ func (m *Composer) Build(name string, dir string) (*helm.Chart, error) { relationships := make(map[string][]interface{}) valMap["relationships"] = &relationships - toRels := m.Constellation.FindRelationshipByToName(n.ID) - fromRels := m.Constellation.FindRelationshipByFromName(n.ID) + toRels := c.Constellation.FindRelationshipByToName(n.ID) + fromRels := c.Constellation.FindRelationshipByFromName(n.ID) for _, i := range toRels { toRelations := make(map[string]string) relationships["input"] = append(relationships["input"], &toRelations) //find the target service - foundService := m.Constellation.FindService(i.From) + foundService := c.Constellation.FindService(i.From) if foundService == nil { return nil, fmt.Errorf("Service '%v' referenced in relationship '%v' not found", i.From, i.ID) @@ -96,7 +96,7 @@ func (m *Composer) Build(name string, dir string) (*helm.Chart, error) { relationships["output"] = append(relationships["output"], &fromRelations) //find the target service - foundService := m.Constellation.FindService(i.To) + foundService := c.Constellation.FindService(i.To) if foundService == nil { return nil, fmt.Errorf("Service '%v' referenced in relationship '%v' not found", i.To, i.ID) @@ -121,19 +121,10 @@ func (m *Composer) Build(name string, dir string) (*helm.Chart, error) { } //LoadFile takes a string dag and map and loads them -func (m *Composer) LoadFile(dagFile string, mapFile string) (err error) { - err = m.Constellation.LoadFile(dagFile) +func (c *Composer) LoadFile(dagFile string, mapFile string) (err error) { + err = c.Constellation.LoadFile(dagFile) if err != nil { return err } - return m.Mapper.LoadFile(mapFile) -} - -//LoadString takes a string dag and map and loads them -func (m *Composer) LoadString(dagString string, mapString string) (err error) { - err = m.Constellation.LoadString(dagString) - if err != nil { - return - } - return m.Mapper.LoadString(mapString) + return c.Mapper.LoadFile(mapFile) } diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go index 657d65f..0705072 100644 --- a/internal/compose/compose_test.go +++ b/internal/compose/compose_test.go @@ -31,7 +31,10 @@ func TestComposeService(t *testing.T) { assert.Error(t, err, "Compose should fail if not yet loaded") - _ = comp.LoadString(test01DagStr, configMapTest01String) + dag := "testdata/constellation.yaml" + mapper := "testdata/mapper.yaml" + + _ = comp.LoadFile(dag, mapper) h, err := comp.Build("test", tdir) @@ -109,70 +112,15 @@ func TestHelmLibCompose(t *testing.T) { func TestLoadFromString(t *testing.T) { comp := new(compose.Composer) - err := comp.LoadString(test01DagStr, configMapTest01String) + dag := "testdata/constellation.yaml" + mapper := "testdata/mapper.yaml" + + err := comp.LoadFile(dag, mapper) assert.NoErrorf(t, err, "Error: %v", err) - err = comp.LoadString("sfdsd", configMapTest01String) + err = comp.LoadFile("sfdsd", mapper) assert.Error(t, err, "Didn't get error when should") - err = comp.LoadString(test01DagStr, "sdfsdf") + err = comp.LoadFile(dag, "sdfsdf") assert.Error(t, err, "Didn't get error when should") } - -const test01DagStr = `Name: "Azure Event Hubs Sample" -Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" -Services: -- Name: "Event Generator" - Id: "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" - Type: "EventGenerator" - Properties: {} -- Name: "Azure Event Hub" - Id: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - Type: "EventHub" - Properties: {} -- Name: "Event Logger" - Id: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" - Type: "EventLogger" - Properties: {} -- Name: "Event Logger" - Id: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" - Type: "EventLogger" - Properties: {} -Relationships: -- Name: "Generator to Event Hubs Link" - Id: "211a55bd-5d92-446c-8be8-190f8f0e623e" - Description: "Event Generator to Event Hub connection" - From: "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" - To: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - Properties: {} -- Name: "Event Hubs to Event Logger Link" - Id: "08ccbd67-456f-4349-854a-4e6959e5017b" - Description: "Event Hubs to Event Logger connection" - From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - To: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" - Properties: {} -- Name: "Event Hubs to Event Logger Link Repeat" - Id: "c8a719e0-164d-408f-9ed1-06e08dc5abbe" - Description: "Event Hubs to Event Logger connection" - From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" - To: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" - Properties: {} -` - -const configMapTest01String = ` -Name: "Basic Azure Event Hubs maps" -Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" -Maps: -- ChartName: "event_hub_sample_event_generator" - Type: "EventGenerator" - Location: "../../helm/basictest" - Version: "1.0.0" -- ChartName: "event_hub_sample_event_logger" - Type: "EventLogger" - Location: "../../helm/basictest" - Version: "1.0.0" -- ChartName: "event_hub_sample_event_hub" - Type: "EventHub" - Location: "../../helm/basictest" - Version: "1.0.0" -` diff --git a/internal/compose/testdata/constellation.yaml b/internal/compose/testdata/constellation.yaml new file mode 100644 index 0000000..493c280 --- /dev/null +++ b/internal/compose/testdata/constellation.yaml @@ -0,0 +1,38 @@ +Name: "Azure Event Hubs Sample" +Id: "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" +Services: +- Name: "Event Generator" + Id: "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" + Type: "EventGenerator" + Properties: {} +- Name: "Azure Event Hub" + Id: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + Type: "EventHub" + Properties: {} +- Name: "Event Logger" + Id: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" + Type: "EventLogger" + Properties: {} +- Name: "Event Logger" + Id: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" + Type: "EventLogger" + Properties: {} +Relationships: +- Name: "Generator to Event Hubs Link" + Id: "211a55bd-5d92-446c-8be8-190f8f0e623e" + Description: "Event Generator to Event Hub connection" + From: "9e1bcb3d-ff58-41d4-8779-f71e7b8800f8" + To: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + Properties: {} +- Name: "Event Hubs to Event Logger Link" + Id: "08ccbd67-456f-4349-854a-4e6959e5017b" + Description: "Event Hubs to Event Logger connection" + From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + To: "1d0255d4-5b8c-4a52-b0bb-ac024cda37e5" + Properties: {} +- Name: "Event Hubs to Event Logger Link Repeat" + Id: "c8a719e0-164d-408f-9ed1-06e08dc5abbe" + Description: "Event Hubs to Event Logger connection" + From: "3aa1e546-1ed5-4d67-a59c-be0d5905b490" + To: "a268fae5-2a82-4a3e-ada7-a52eeb7019ac" + Properties: {} diff --git a/internal/compose/testdata/mapper.yaml b/internal/compose/testdata/mapper.yaml new file mode 100644 index 0000000..819f684 --- /dev/null +++ b/internal/compose/testdata/mapper.yaml @@ -0,0 +1,15 @@ +Name: "Basic Azure Event Hubs maps" +Id: "a5a7c413-a020-44a2-bd23-1941adb7ad58" +Maps: +- ChartName: "event_hub_sample_event_generator" + Type: "EventGenerator" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_logger" + Type: "EventLogger" + Location: "../../helm/basictest" + Version: "1.0.0" +- ChartName: "event_hub_sample_event_hub" + Type: "EventHub" + Location: "../../helm/basictest" + Version: "1.0.0" From 03b0e1ac12de22d463ac4c7fc345d220b72c21ee Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 15:05:33 +1100 Subject: [PATCH 44/85] Changed Set to Compare. --- cmd/diff.go | 2 +- internal/diff/diff.go | 8 ++++---- internal/diff/diff_test.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/diff.go b/cmd/diff.go index 1e95d39..0da5a52 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -82,7 +82,7 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP logger.Output(resStringNew) } - constellationSets := diff.Set{Original: dsGraphOrg, New: dsGraphNew} + constellationSets := diff.Compare{Original: dsGraphOrg, New: dsGraphNew} resStringDiff, err := constellationSets.CompareConstellations() if err != nil { diff --git a/internal/diff/diff.go b/internal/diff/diff.go index 239da88..6773cdd 100644 --- a/internal/diff/diff.go +++ b/internal/diff/diff.go @@ -18,14 +18,14 @@ type ComparisonSet struct { SetDelRels set.Set } -// Set contains two constellation configurations to compare against one another. -type Set struct { +// Compare contains two constellation configurations to compare against one another. +type Compare struct { Original *constellation.Config New *constellation.Config } //CompareConstellations takes two constellation configurations, compares and returns the differences. -func (d *Set) CompareConstellations() (string, error) { +func (d *Compare) CompareConstellations() (string, error) { // populate comparison sets with changes between original and new graph sets := d.FillComparisonSets() @@ -35,7 +35,7 @@ func (d *Set) CompareConstellations() (string, error) { // FillComparisonSets - loads provided set struct with data from the constellations and then determines the various differences between the // sets (original constellation and new) to help detemine what has been added, removed or changed. -func (d *Set) FillComparisonSets() (sets ComparisonSet) { +func (d *Compare) FillComparisonSets() (sets ComparisonSet) { setOrgSvcs, setOrgRel := createSet(d.Original) setNewSvcs, setNewRel := createSet(d.New) diff --git a/internal/diff/diff_test.go b/internal/diff/diff_test.go index 83dba77..5bdfedc 100644 --- a/internal/diff/diff_test.go +++ b/internal/diff/diff_test.go @@ -27,7 +27,7 @@ func TestGetComparisonSets(t *testing.T) { populateComparisonSets(knownSets) // function being tested - diffSet := diff.Set{Original: dsGraphOrg, New: dsGraphNew} + diffSet := diff.Compare{Original: dsGraphOrg, New: dsGraphNew} loadedSets := diffSet.FillComparisonSets() assert.True(t, knownSets.SetCommonSvcs.Equal(loadedSets.SetCommonSvcs), "Common services - did not match between expected result and input yaml") @@ -52,7 +52,7 @@ func TestGraphWithChanges(t *testing.T) { assert.NoErrorf(t, err, "dagConfigService failed to load file %s", err) // function being tested - diffSet := diff.Set{Original: dsGraphOrg, New: dsGraphNew} + diffSet := diff.Compare{Original: dsGraphOrg, New: dsGraphNew} loadedSets := diffSet.FillComparisonSets() resString, err := diff.CreateGraphWithChanges(dsGraphNew, &loadedSets) From b74df211743675184f91655c70f9d50b7bc31b98 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 15:23:05 +1100 Subject: [PATCH 45/85] Moved tools out from internal to root of project. --- cmd/commands.go | 2 +- cmd/compose.go | 2 +- cmd/compose_test.go | 2 +- cmd/diff.go | 4 ++-- cmd/diff_test.go | 2 +- cmd/validate.go | 4 ++-- cmd/validate_test.go | 2 +- cmd/version.go | 2 +- cmd/version_test.go | 2 +- cmd/visualize.go | 4 ++-- cmd/visualize_test.go | 4 ++-- internal/diff/diff_test.go | 2 +- internal/platform/constellation/constellation.go | 2 +- internal/platform/constellation/constellation_test.go | 2 +- internal/platform/constellation/find.go | 2 +- internal/platform/constellation/graph_test.go | 2 +- internal/platform/constellation/validate.go | 2 +- internal/platform/mapper/find.go | 2 +- internal/platform/mapper/mapper.go | 2 +- internal/platform/mapper/mapper_test.go | 2 +- internal/platform/mapper/validate.go | 2 +- main.go | 2 +- {internal/tools => tools}/file/file.go | 0 {internal/tools => tools}/file/file_test.go | 4 ++-- {internal/tools => tools}/find/find.go | 0 {internal/tools => tools}/find/find_test.go | 2 +- {internal/tools => tools}/guid/guid.go | 0 {internal/tools => tools}/guid/guid_test.go | 2 +- {internal/tools => tools}/logger/formatter.go | 0 {internal/tools => tools}/logger/logger.go | 0 {internal/tools => tools}/test/test.go | 0 31 files changed, 30 insertions(+), 30 deletions(-) rename {internal/tools => tools}/file/file.go (100%) rename {internal/tools => tools}/file/file_test.go (76%) rename {internal/tools => tools}/find/find.go (100%) rename {internal/tools => tools}/find/find_test.go (90%) rename {internal/tools => tools}/guid/guid.go (100%) rename {internal/tools => tools}/guid/guid_test.go (96%) rename {internal/tools => tools}/logger/formatter.go (100%) rename {internal/tools => tools}/logger/logger.go (100%) rename {internal/tools => tools}/test/test.go (100%) diff --git a/cmd/commands.go b/cmd/commands.go index a110076..0d09514 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/microsoft/abstrakt/internal/tools/logger" + "github.com/microsoft/abstrakt/tools/logger" cobra "github.com/spf13/cobra" ) diff --git a/cmd/compose.go b/cmd/compose.go index a7d4d5c..de5d365 100644 --- a/cmd/compose.go +++ b/cmd/compose.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/microsoft/abstrakt/internal/compose" "github.com/microsoft/abstrakt/internal/platform/chart" - "github.com/microsoft/abstrakt/internal/tools/logger" + "github.com/microsoft/abstrakt/tools/logger" "github.com/spf13/cobra" "path" "strings" diff --git a/cmd/compose_test.go b/cmd/compose_test.go index c340e95..819961a 100644 --- a/cmd/compose_test.go +++ b/cmd/compose_test.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/microsoft/abstrakt/internal/tools/test" + "github.com/microsoft/abstrakt/tools/test" "github.com/stretchr/testify/assert" "testing" ) diff --git a/cmd/diff.go b/cmd/diff.go index 0da5a52..b0941a5 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -10,8 +10,8 @@ import ( "fmt" "github.com/microsoft/abstrakt/internal/diff" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/file" - "github.com/microsoft/abstrakt/internal/tools/logger" + "github.com/microsoft/abstrakt/tools/file" + "github.com/microsoft/abstrakt/tools/logger" "github.com/spf13/cobra" "strings" ) diff --git a/cmd/diff_test.go b/cmd/diff_test.go index 71f06d1..9963e05 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -1,7 +1,7 @@ package cmd import ( - helper "github.com/microsoft/abstrakt/internal/tools/test" + helper "github.com/microsoft/abstrakt/tools/test" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" "testing" diff --git a/cmd/validate.go b/cmd/validate.go index 16902a9..9bf6368 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/microsoft/abstrakt/internal/platform/constellation" "github.com/microsoft/abstrakt/internal/platform/mapper" - "github.com/microsoft/abstrakt/internal/tools/find" - "github.com/microsoft/abstrakt/internal/tools/logger" + "github.com/microsoft/abstrakt/tools/find" + "github.com/microsoft/abstrakt/tools/logger" "github.com/spf13/cobra" ) diff --git a/cmd/validate_test.go b/cmd/validate_test.go index b3a3145..9f77060 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -1,7 +1,7 @@ package cmd import ( - helper "github.com/microsoft/abstrakt/internal/tools/test" + helper "github.com/microsoft/abstrakt/tools/test" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" "testing" diff --git a/cmd/version.go b/cmd/version.go index 326f4a8..708a30e 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,7 +1,7 @@ package cmd import ( - logger "github.com/microsoft/abstrakt/internal/tools/logger" + logger "github.com/microsoft/abstrakt/tools/logger" "github.com/spf13/cobra" ) diff --git a/cmd/version_test.go b/cmd/version_test.go index 63e26ee..aba3406 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -1,7 +1,7 @@ package cmd import ( - helper "github.com/microsoft/abstrakt/internal/tools/test" + helper "github.com/microsoft/abstrakt/tools/test" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" "os" diff --git a/cmd/visualize.go b/cmd/visualize.go index bd1ae85..c134313 100644 --- a/cmd/visualize.go +++ b/cmd/visualize.go @@ -9,8 +9,8 @@ import ( "bytes" "fmt" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/file" - "github.com/microsoft/abstrakt/internal/tools/logger" + "github.com/microsoft/abstrakt/tools/file" + "github.com/microsoft/abstrakt/tools/logger" "github.com/spf13/cobra" "strings" ) diff --git a/cmd/visualize_test.go b/cmd/visualize_test.go index c1c0384..13f663c 100644 --- a/cmd/visualize_test.go +++ b/cmd/visualize_test.go @@ -2,8 +2,8 @@ package cmd import ( "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/file" - helper "github.com/microsoft/abstrakt/internal/tools/test" + "github.com/microsoft/abstrakt/tools/file" + helper "github.com/microsoft/abstrakt/tools/test" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" "io/ioutil" diff --git a/internal/diff/diff_test.go b/internal/diff/diff_test.go index 5bdfedc..d0837cf 100644 --- a/internal/diff/diff_test.go +++ b/internal/diff/diff_test.go @@ -4,7 +4,7 @@ import ( set "github.com/deckarep/golang-set" "github.com/microsoft/abstrakt/internal/diff" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/test" + "github.com/microsoft/abstrakt/tools/test" "github.com/stretchr/testify/assert" "testing" ) diff --git a/internal/platform/constellation/constellation.go b/internal/platform/constellation/constellation.go index 1771e04..2cebab8 100644 --- a/internal/platform/constellation/constellation.go +++ b/internal/platform/constellation/constellation.go @@ -14,7 +14,7 @@ package constellation //////////////////////////////////////////////////////////// import ( - "github.com/microsoft/abstrakt/internal/tools/guid" + "github.com/microsoft/abstrakt/tools/guid" yamlParser "gopkg.in/yaml.v2" "io/ioutil" "reflect" diff --git a/internal/platform/constellation/constellation_test.go b/internal/platform/constellation/constellation_test.go index ae5530e..bf37fe0 100644 --- a/internal/platform/constellation/constellation_test.go +++ b/internal/platform/constellation/constellation_test.go @@ -2,7 +2,7 @@ package constellation_test import ( "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/guid" + "github.com/microsoft/abstrakt/tools/guid" "github.com/stretchr/testify/assert" "io/ioutil" "reflect" diff --git a/internal/platform/constellation/find.go b/internal/platform/constellation/find.go index 0fc9dab..397d55a 100644 --- a/internal/platform/constellation/find.go +++ b/internal/platform/constellation/find.go @@ -1,7 +1,7 @@ package constellation import ( - "github.com/microsoft/abstrakt/internal/tools/guid" + "github.com/microsoft/abstrakt/tools/guid" "strings" ) diff --git a/internal/platform/constellation/graph_test.go b/internal/platform/constellation/graph_test.go index c9180d7..c3ec53b 100644 --- a/internal/platform/constellation/graph_test.go +++ b/internal/platform/constellation/graph_test.go @@ -3,7 +3,7 @@ package constellation_test import ( "bytes" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/internal/tools/test" + "github.com/microsoft/abstrakt/tools/test" "github.com/stretchr/testify/assert" "testing" ) diff --git a/internal/platform/constellation/validate.go b/internal/platform/constellation/validate.go index dceda3b..485cee9 100644 --- a/internal/platform/constellation/validate.go +++ b/internal/platform/constellation/validate.go @@ -1,7 +1,7 @@ package constellation import ( - "github.com/microsoft/abstrakt/internal/tools/find" + "github.com/microsoft/abstrakt/tools/find" "gopkg.in/dealancer/validate.v2" ) diff --git a/internal/platform/mapper/find.go b/internal/platform/mapper/find.go index 377f528..1f4bd20 100644 --- a/internal/platform/mapper/find.go +++ b/internal/platform/mapper/find.go @@ -1,7 +1,7 @@ package mapper import ( - "github.com/microsoft/abstrakt/internal/tools/guid" + "github.com/microsoft/abstrakt/tools/guid" "strings" ) diff --git a/internal/platform/mapper/mapper.go b/internal/platform/mapper/mapper.go index 6b06455..5ac07b8 100644 --- a/internal/platform/mapper/mapper.go +++ b/internal/platform/mapper/mapper.go @@ -7,7 +7,7 @@ package mapper ////////////////////////////////////////////////////// import ( - "github.com/microsoft/abstrakt/internal/tools/guid" + "github.com/microsoft/abstrakt/tools/guid" yamlParser "gopkg.in/yaml.v2" "io/ioutil" "reflect" diff --git a/internal/platform/mapper/mapper_test.go b/internal/platform/mapper/mapper_test.go index 3cc0d07..7faa3fc 100644 --- a/internal/platform/mapper/mapper_test.go +++ b/internal/platform/mapper/mapper_test.go @@ -2,7 +2,7 @@ package mapper_test import ( "github.com/microsoft/abstrakt/internal/platform/mapper" - "github.com/microsoft/abstrakt/internal/tools/guid" + "github.com/microsoft/abstrakt/tools/guid" "github.com/stretchr/testify/assert" "io/ioutil" "reflect" diff --git a/internal/platform/mapper/validate.go b/internal/platform/mapper/validate.go index 3474719..213eba7 100644 --- a/internal/platform/mapper/validate.go +++ b/internal/platform/mapper/validate.go @@ -1,7 +1,7 @@ package mapper import ( - "github.com/microsoft/abstrakt/internal/tools/find" + "github.com/microsoft/abstrakt/tools/find" "gopkg.in/dealancer/validate.v2" ) diff --git a/main.go b/main.go index c082702..19d9b7a 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ package main import ( cmd "github.com/microsoft/abstrakt/cmd" - "github.com/microsoft/abstrakt/internal/tools/logger" + "github.com/microsoft/abstrakt/tools/logger" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" diff --git a/internal/tools/file/file.go b/tools/file/file.go similarity index 100% rename from internal/tools/file/file.go rename to tools/file/file.go diff --git a/internal/tools/file/file_test.go b/tools/file/file_test.go similarity index 76% rename from internal/tools/file/file_test.go rename to tools/file/file_test.go index edbdb5b..c6021fe 100644 --- a/internal/tools/file/file_test.go +++ b/tools/file/file_test.go @@ -1,7 +1,7 @@ package file_test import ( - "github.com/microsoft/abstrakt/internal/tools/file" + "github.com/microsoft/abstrakt/tools/file" "github.com/stretchr/testify/assert" "testing" ) @@ -11,7 +11,7 @@ func TestFileExistsNoFile(t *testing.T) { } func TestFileExists(t *testing.T) { - assert.True(t, file.Exists("../../../README.md")) + assert.True(t, file.Exists("../../README.md")) } func TestFileExistsIsFolder(t *testing.T) { diff --git a/internal/tools/find/find.go b/tools/find/find.go similarity index 100% rename from internal/tools/find/find.go rename to tools/find/find.go diff --git a/internal/tools/find/find_test.go b/tools/find/find_test.go similarity index 90% rename from internal/tools/find/find_test.go rename to tools/find/find_test.go index b069f52..8769e17 100644 --- a/internal/tools/find/find_test.go +++ b/tools/find/find_test.go @@ -1,7 +1,7 @@ package find_test import ( - "github.com/microsoft/abstrakt/internal/tools/find" + "github.com/microsoft/abstrakt/tools/find" "github.com/stretchr/testify/assert" "testing" ) diff --git a/internal/tools/guid/guid.go b/tools/guid/guid.go similarity index 100% rename from internal/tools/guid/guid.go rename to tools/guid/guid.go diff --git a/internal/tools/guid/guid_test.go b/tools/guid/guid_test.go similarity index 96% rename from internal/tools/guid/guid_test.go rename to tools/guid/guid_test.go index a9bfddf..39aedf2 100644 --- a/internal/tools/guid/guid_test.go +++ b/tools/guid/guid_test.go @@ -1,7 +1,7 @@ package guid_test import ( - "github.com/microsoft/abstrakt/internal/tools/guid" + "github.com/microsoft/abstrakt/tools/guid" "testing" ) diff --git a/internal/tools/logger/formatter.go b/tools/logger/formatter.go similarity index 100% rename from internal/tools/logger/formatter.go rename to tools/logger/formatter.go diff --git a/internal/tools/logger/logger.go b/tools/logger/logger.go similarity index 100% rename from internal/tools/logger/logger.go rename to tools/logger/logger.go diff --git a/internal/tools/test/test.go b/tools/test/test.go similarity index 100% rename from internal/tools/test/test.go rename to tools/test/test.go From 022652a91f8896c649b3f2bde3ea1cd400f0101b Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 18:30:47 +1100 Subject: [PATCH 46/85] Unwrap error in graph.go. --- internal/platform/constellation/graph.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/platform/constellation/graph.go b/internal/platform/constellation/graph.go index 8763866..7109205 100644 --- a/internal/platform/constellation/graph.go +++ b/internal/platform/constellation/graph.go @@ -20,15 +20,15 @@ func (readGraph *Config) GenerateGraph(out io.Writer) (string, error) { // Replace spaces with underscores, names with spaces can break graphviz engines if err := g.SetName(strings.Replace(readGraph.Name, " ", "_", -1)); err != nil { - return "", fmt.Errorf("error: %v", err) + return "", err } if err := g.AddAttr(g.Name, "rankdir", "LR"); err != nil { - return "", fmt.Errorf("error adding attribute: %v", err) + return "", err } // Make the graph directed (a constellation is DAG) if err := g.SetDir(true); err != nil { - return "", fmt.Errorf("error: %v", err) + return "", err } // Add all nodes to the graph storing the lookup from ID to name (for later adding relationships) @@ -40,10 +40,11 @@ func (readGraph *Config) GenerateGraph(out io.Writer) (string, error) { if strings.Compare(newName, v.ID) != 0 { fmt.Fprintf(out, "Changing %s to %s\n", v.ID, newName) } + lookup[v.ID] = newName err := g.AddNode(readGraph.Name, "\""+newName+"\"", nil) if err != nil { - return "", fmt.Errorf("error: %v", err) + return "", err } } @@ -55,7 +56,7 @@ func (readGraph *Config) GenerateGraph(out io.Writer) (string, error) { localTo := "\"" + lookup[v.To] + "\"" err := g.AddEdge(localFrom, localTo, true, nil) if err != nil { - return "", fmt.Errorf("error: %v", err) + return "", err } } From 59b00d75cde4c64ceb279ca3d3c049e65df04862 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 18:30:54 +1100 Subject: [PATCH 47/85] Cleanup. --- internal/platform/constellation/constellation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/platform/constellation/constellation.go b/internal/platform/constellation/constellation.go index 2cebab8..bd90e29 100644 --- a/internal/platform/constellation/constellation.go +++ b/internal/platform/constellation/constellation.go @@ -67,7 +67,7 @@ func (m *Config) LoadFile(fileName string) (err error) { } // LoadString -- New DAG info instance from the given yaml string. -func (m *Config) LoadString(yamlString string) (err error) { +func (m *Config) LoadString(yamlString string) error { return yamlParser.Unmarshal([]byte(yamlString), m) } From d84df02a4e6340283917cd587fc5b3f2f8adf403 Mon Sep 17 00:00:00 2001 From: JasonTheDeveloper Date: Thu, 23 Jan 2020 18:52:28 +1100 Subject: [PATCH 48/85] Update azure-pipelines.yaml for Azure Pipelines --- build/ci/azure-pipelines.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build/ci/azure-pipelines.yaml b/build/ci/azure-pipelines.yaml index 3a1096b..c5f4105 100644 --- a/build/ci/azure-pipelines.yaml +++ b/build/ci/azure-pipelines.yaml @@ -1,4 +1,5 @@ trigger: + branches: include: - master @@ -20,6 +21,12 @@ steps: version: '1.13' GOPATH: '$(GOPATH)' GOBIN: '$(GOBIN)' + +- task: Go@0 + displayName: 'Update Go Packages' + inputs: + command: 'get' + arguments: '-u all' - task: Go@0 displayName: 'Resolving Dependencies' From 5a9aae4d774f0318a98a76f995f4991785e6d2ee Mon Sep 17 00:00:00 2001 From: JasonTheDeveloper Date: Thu, 23 Jan 2020 18:59:22 +1100 Subject: [PATCH 49/85] Update azure-pipelines.yaml for Azure Pipelines --- build/ci/azure-pipelines.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build/ci/azure-pipelines.yaml b/build/ci/azure-pipelines.yaml index c5f4105..cd14200 100644 --- a/build/ci/azure-pipelines.yaml +++ b/build/ci/azure-pipelines.yaml @@ -27,7 +27,8 @@ steps: inputs: command: 'get' arguments: '-u all' - + workingDirectory: '$(System.DefaultWorkingDirectory)' + - task: Go@0 displayName: 'Resolving Dependencies' inputs: From 4c9bfe5892096e3ae1fab5da3ba67c875434da74 Mon Sep 17 00:00:00 2001 From: JasonTheDeveloper Date: Thu, 23 Jan 2020 19:19:32 +1100 Subject: [PATCH 50/85] Update azure-pipelines.yaml for Azure Pipelines --- build/ci/azure-pipelines.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/ci/azure-pipelines.yaml b/build/ci/azure-pipelines.yaml index cd14200..b91a126 100644 --- a/build/ci/azure-pipelines.yaml +++ b/build/ci/azure-pipelines.yaml @@ -23,10 +23,10 @@ steps: GOBIN: '$(GOBIN)' - task: Go@0 - displayName: 'Update Go Packages' + displayName: 'Update kubectl Package' inputs: command: 'get' - arguments: '-u all' + arguments: '-u k8s.io/kubectl' workingDirectory: '$(System.DefaultWorkingDirectory)' - task: Go@0 From 35c5dc576ece0c28cb96bf098e80e4aa6fd6205d Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Jan 2020 19:30:28 +1100 Subject: [PATCH 51/85] Removed task. --- build/ci/azure-pipelines.yaml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/build/ci/azure-pipelines.yaml b/build/ci/azure-pipelines.yaml index b91a126..9339589 100644 --- a/build/ci/azure-pipelines.yaml +++ b/build/ci/azure-pipelines.yaml @@ -22,13 +22,6 @@ steps: GOPATH: '$(GOPATH)' GOBIN: '$(GOBIN)' -- task: Go@0 - displayName: 'Update kubectl Package' - inputs: - command: 'get' - arguments: '-u k8s.io/kubectl' - workingDirectory: '$(System.DefaultWorkingDirectory)' - - task: Go@0 displayName: 'Resolving Dependencies' inputs: From 7cde51493d0c2cac9b028e978472d3635bd8f528 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 24 Jan 2020 11:21:56 +1100 Subject: [PATCH 52/85] Undid US spelling and replace linter misspell to UK. --- .golangci.yml | 4 +++- cmd/commands.go | 2 +- cmd/diff.go | 4 ++-- cmd/{visualize.go => visualise.go} | 16 ++++++------- cmd/{visualize_test.go => visualise_test.go} | 14 ++++++------ docs/usage.md | 24 ++++++++++---------- internal/compose/compose.go | 2 +- internal/diff/diff.go | 4 ++-- internal/platform/constellation/graph.go | 2 +- makefile | 8 +++---- 10 files changed, 41 insertions(+), 39 deletions(-) rename cmd/{visualize.go => visualise.go} (80%) rename cmd/{visualize_test.go => visualise_test.go} (88%) diff --git a/.golangci.yml b/.golangci.yml index b29107e..fa5b38c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -12,11 +12,13 @@ linters-settings: min-len: 3.0 min-occurrences: 3.0 misspell: - locale: "US" + locale: "UK" ignore-words: - listend - analyses - cancelling + - color + - colors linters: enable: - vet diff --git a/cmd/commands.go b/cmd/commands.go index 0d09514..26e5979 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -52,7 +52,7 @@ func DefaultRootCommand() *cobra.Command { return nil } - addCommands(c, newComposeCmd(), newVersionCmd(), newVisualizeCmd(), newValidateCmd(), newDiffCmd()) + addCommands(c, newComposeCmd(), newVersionCmd(), newVisualiseCmd(), newValidateCmd(), newDiffCmd()) return c } diff --git a/cmd/diff.go b/cmd/diff.go index b0941a5..2470556 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -1,9 +1,9 @@ package cmd -// visualize is a subcommand that constructs a graph representation of the yaml +// visualise is a subcommand that constructs a graph representation of the yaml // input file and renders this into GraphViz 'dot' notation. // Initial version renders to dot syntax only, to graphically depict this the output -// has to be run through a graphviz visualization tool/utiliyy +// has to be run through a graphviz visualisation tool/utiliyy import ( "bytes" diff --git a/cmd/visualize.go b/cmd/visualise.go similarity index 80% rename from cmd/visualize.go rename to cmd/visualise.go index c134313..8361d9c 100644 --- a/cmd/visualize.go +++ b/cmd/visualise.go @@ -1,9 +1,9 @@ package cmd -// visualize is a subcommand that constructs a graph representation of the yaml +// visualise is a subcommand that constructs a graph representation of the yaml // input file and renders this into GraphViz 'dot' notation. // Initial version renders to dot syntax only, to graphically depict this the output -// has to be run through a graphviz visualization tool/utiliyy +// has to be run through a graphviz visualisation tool/utiliyy import ( "bytes" @@ -15,20 +15,20 @@ import ( "strings" ) -type visualizeCmd struct { +type visualiseCmd struct { constellationFilePath string *baseCmd } -func newVisualizeCmd() *visualizeCmd { - cc := &visualizeCmd{} +func newVisualiseCmd() *visualiseCmd { + cc := &visualiseCmd{} cc.baseCmd = newBaseCmd(&cobra.Command{ - Use: "visualize", + Use: "visualise", Short: "Format a constellation configuration as Graphviz dot notation", - Long: `Visualize is for producing Graphviz dot notation code of a constellation configuration + Long: `Visualise is for producing Graphviz dot notation code of a constellation configuration -Example: abstrakt visualize -f [constellationFilePath]`, +Example: abstrakt visualise -f [constellationFilePath]`, RunE: func(cmd *cobra.Command, args []string) error { logger.Debug("args: " + strings.Join(args, " ")) diff --git a/cmd/visualize_test.go b/cmd/visualise_test.go similarity index 88% rename from cmd/visualize_test.go rename to cmd/visualise_test.go index 13f663c..b54c010 100644 --- a/cmd/visualize_test.go +++ b/cmd/visualise_test.go @@ -12,11 +12,11 @@ import ( "testing" ) -func TestVisualizeCmdWithAllRequirementsNoError(t *testing.T) { +func TestVisualiseCmdWithAllRequirementsNoError(t *testing.T) { constellationPath := "testdata/constellation/valid.yaml" hook := test.NewGlobal() - _, err := helper.ExecuteCommand(newVisualizeCmd().cmd, "-f", constellationPath) + _, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", constellationPath) if err != nil { t.Error("Did not receive output") @@ -25,10 +25,10 @@ func TestVisualizeCmdWithAllRequirementsNoError(t *testing.T) { } } -func TestVisualizeCmdFailYaml(t *testing.T) { +func TestVisualiseCmdFailYaml(t *testing.T) { expected := "Could not open YAML input file for reading" - output, err := helper.ExecuteCommand(newVisualizeCmd().cmd, "-f", "constellationPath") + output, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", "constellationPath") if err != nil { assert.Contains(t, err.Error(), expected) @@ -37,10 +37,10 @@ func TestVisualizeCmdFailYaml(t *testing.T) { } } -func TestVisualizeCmdFailNotYaml(t *testing.T) { +func TestVisualiseCmdFailNotYaml(t *testing.T) { expected := "dagConfigService failed to load file" - output, err := helper.ExecuteCommand(newVisualizeCmd().cmd, "-f", "visualize.go") + output, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", "visualise.go") if err != nil { assert.Contains(t, err.Error(), expected) @@ -55,7 +55,7 @@ func TestFileExists(t *testing.T) { defer helper.CleanTempTestFiles(t, tdir) //Setup variables and content for test - testValidFilename := filepath.Join(tdir, "testVisualize.out") + testValidFilename := filepath.Join(tdir, "testVisualise.out") testInvalidFilename := filepath.Join(tdir, "nonexistant.out") testData := []byte("A file to test with") diff --git a/docs/usage.md b/docs/usage.md index ea7da73..c4888aa 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -15,7 +15,7 @@ Available Commands: help Help about any command validate Validate a constellation file for correct schema and ensure correctness. version The version of Abstrakt being used - visualize Format a constellation configuration as Graphviz dot notation + visualise Format a constellation configuration as Graphviz dot notation Flags: -h, --help help for abstrakt @@ -80,27 +80,27 @@ Global Flags: -v, --verbose Use verbose output logs ``` -### abstrakt `visualize` +### abstrakt `visualise` ```bash -Visualize is for producing Graphviz dot notation code of a constellation configuration +Visualise is for producing Graphviz dot notation code of a constellation configuration -Example: abstrakt visualize -f [constellationFilePath] +Example: abstrakt visualise -f [constellationFilePath] Usage: - abstrakt visualize [flags] + abstrakt visualise [flags] Flags: -f, --constellationFilePath string constellation file path - -h, --help help for visualize + -h, --help help for visualise Global Flags: -v, --verbose Use verbose output logs ``` -The output from the visualize subcommand is [Graphviz dot notation](https://www.graphviz.org/doc/info/lang.html) +The output from the visualise subcommand is [Graphviz dot notation](https://www.graphviz.org/doc/info/lang.html) -The output from a call to 'abstrakt visualize' can be piped into Graphviz to generate a graphical output. See the example in the Examples section. +The output from a call to 'abstrakt visualise' can be piped into Graphviz to generate a graphical output. See the example in the Examples section. Alternatively, copy the output and paste into a Graphviz rendering tool to see the graph produced. Some sites listed below (rendering option in the utility to be developed). @@ -129,9 +129,9 @@ Global Flags: #### Examples -Run visualize on a file +Run visualise on a file - abstrakt visualize -f basic_azure_event_hubs.yaml + abstrakt visualise -f basic_azure_event_hubs.yaml digraph Azure_Event_Hubs_Sample { Event_Generator->Azure_Event_Hub; Azure_Event_Hub->Event_Logger; @@ -141,6 +141,6 @@ Run visualize on a file } -Pipe visualize output to Graphviz producing a file called result.png (assumes Graphviz is installed and can be called from the location abstrakt is being run) +Pipe visualise output to Graphviz producing a file called result.png (assumes Graphviz is installed and can be called from the location abstrakt is being run) - abstrakt visualize -f ./examples/constellation/sample_consteallation.yaml | dot -Tpng > result.png \ No newline at end of file + abstrakt visualise -f ./examples/constellation/sample_consteallation.yaml | dot -Tpng > result.png \ No newline at end of file diff --git a/internal/compose/compose.go b/internal/compose/compose.go index cb208f7..e96160a 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -17,7 +17,7 @@ type Composer struct { //Build takes the loaded DAG and maps and builds the Helm values and requirements documents func (c *Composer) Build(name string, dir string) (*helm.Chart, error) { if c.Constellation.Name == "" || c.Mapper.Name == "" { - return nil, fmt.Errorf("Please initialize with LoadFromFile or LoadFromString") + return nil, fmt.Errorf("Please initialise with LoadFromFile or LoadFromString") } newChart, err := chart.Create(name, dir) diff --git a/internal/diff/diff.go b/internal/diff/diff.go index 6773cdd..895aa9a 100644 --- a/internal/diff/diff.go +++ b/internal/diff/diff.go @@ -74,10 +74,10 @@ func createSet(dsGraph *constellation.Config) (set.Set, set.Set) { } // CreateGraphWithChanges - use both input constellations (new and original) as well as the comparison sets to create -// a dag that can be visualized. It uses the comparison sets to identify additions, deletions and changes between the original +// a dag that can be visualised. It uses the comparison sets to identify additions, deletions and changes between the original // and new constellations. func CreateGraphWithChanges(newGraph *constellation.Config, sets *ComparisonSet) (string, error) { - // Lookup is used to map IDs to names. Names are easier to visualize but IDs are more important to ensure the + // Lookup is used to map IDs to names. Names are easier to visualise but IDs are more important to ensure the // presented constellation is correct and IDs are used to link nodes together lookup := make(map[string]string) g := gographviz.NewGraph() diff --git a/internal/platform/constellation/graph.go b/internal/platform/constellation/graph.go index 7109205..a100e43 100644 --- a/internal/platform/constellation/graph.go +++ b/internal/platform/constellation/graph.go @@ -12,7 +12,7 @@ import ( // this can be passed on to GraphViz to graphically render the resulting graph func (readGraph *Config) GenerateGraph(out io.Writer) (string, error) { - // Lookup is used to map IDs to names. Names are easier to visualize but IDs are more important to ensure the + // Lookup is used to map IDs to names. Names are easier to visualise but IDs are more important to ensure the // presented constellation is correct and IDs are used to link nodes together lookup := make(map[string]string) diff --git a/makefile b/makefile index 41919d2..1a5f0c5 100644 --- a/makefile +++ b/makefile @@ -97,18 +97,18 @@ endif fmt: gofmt -s -w ./ -build: +build: go build -o abstrakt main.go -visualize: build - ./abstrakt visualize -f ./examples/constellation/http_constellation.yaml | dot -Tpng > result.png +visualise: build + ./abstrakt visualise -f ./examples/constellation/http_constellation.yaml | dot -Tpng > result.png diff: build ./abstrakt diff -o ./examples/constellation/sample_constellation.yaml -n ./examples/constellation/sample_constellation_changed.yaml | dot -Tpng > result.png run-http-demo: http-demo http-demo-deploy -http-demo: build +http-demo: build ./abstrakt compose http-demo -f ./examples/constellation/http_constellation.yaml -m ./examples/constellation/http_constellation_maps.yaml -o ./output/http_sample http-demo-deploy: From 845c928ea340280cba801e7ebd6aad019735232e Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 24 Jan 2020 11:24:34 +1100 Subject: [PATCH 53/85] Removed irrelevant comment. --- cmd/diff.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cmd/diff.go b/cmd/diff.go index 2470556..7ca2ec8 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -1,10 +1,5 @@ package cmd -// visualise is a subcommand that constructs a graph representation of the yaml -// input file and renders this into GraphViz 'dot' notation. -// Initial version renders to dot syntax only, to graphically depict this the output -// has to be run through a graphviz visualisation tool/utiliyy - import ( "bytes" "fmt" From 55d2012087780d444c8061f3916d1a5dec7e20b4 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 24 Jan 2020 16:09:29 +1100 Subject: [PATCH 54/85] Folded validate.go into other .go files. --- .../platform/constellation/constellation.go | 6 + .../constellation/constellation_test.go | 10 ++ internal/platform/constellation/find.go | 50 +++++++ internal/platform/constellation/find_test.go | 119 +++++++++++++++ internal/platform/constellation/validate.go | 60 -------- .../platform/constellation/validate_test.go | 136 ------------------ internal/platform/mapper/find.go | 49 +++++++ internal/platform/mapper/find_test.go | 111 ++++++++++++++ internal/platform/mapper/mapper.go | 6 + internal/platform/mapper/mapper_test.go | 10 ++ internal/platform/mapper/validate.go | 59 -------- internal/platform/mapper/validate_test.go | 128 ----------------- 12 files changed, 361 insertions(+), 383 deletions(-) delete mode 100644 internal/platform/constellation/validate.go delete mode 100644 internal/platform/constellation/validate_test.go delete mode 100644 internal/platform/mapper/validate.go delete mode 100644 internal/platform/mapper/validate_test.go diff --git a/internal/platform/constellation/constellation.go b/internal/platform/constellation/constellation.go index bd90e29..51f0c44 100644 --- a/internal/platform/constellation/constellation.go +++ b/internal/platform/constellation/constellation.go @@ -15,6 +15,7 @@ package constellation import ( "github.com/microsoft/abstrakt/tools/guid" + "gopkg.in/dealancer/validate.v2" yamlParser "gopkg.in/yaml.v2" "io/ioutil" "reflect" @@ -75,3 +76,8 @@ func (m *Config) LoadString(yamlString string) error { func (m *Config) IsEmpty() bool { return reflect.DeepEqual(Config{}, *m) } + +// ValidateModel checks if constellation has all required felids +func (m *Config) ValidateModel() error { + return validate.Validate(m) +} diff --git a/internal/platform/constellation/constellation_test.go b/internal/platform/constellation/constellation_test.go index bf37fe0..bd76a90 100644 --- a/internal/platform/constellation/constellation_test.go +++ b/internal/platform/constellation/constellation_test.go @@ -62,6 +62,16 @@ func TestIsEmptyFalse(t *testing.T) { assert.False(t, dag.IsEmpty()) } +func TestSchemaChecks(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/valid.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.NoError(t, err, "Model validation should not return errors") +} + var test01WantDag constellation.Config = constellation.Config{ Name: "Azure Event Hubs Sample", ID: guid.GUID("d6e4a5e9-696a-4626-ba7a-534d6ff450a5"), diff --git a/internal/platform/constellation/find.go b/internal/platform/constellation/find.go index 397d55a..cc21d40 100644 --- a/internal/platform/constellation/find.go +++ b/internal/platform/constellation/find.go @@ -1,6 +1,7 @@ package constellation import ( + "github.com/microsoft/abstrakt/tools/find" "github.com/microsoft/abstrakt/tools/guid" "strings" ) @@ -55,3 +56,52 @@ func (m *Config) FindRelationshipByFromName(relationshipFromName string) (res [] } return } + +// FindDuplicateIDs checks for duplicate Relationship and Service IDs in a constellation file. +func (m *Config) FindDuplicateIDs() (duplicates []string) { + IDs := []string{string(m.ID)} + + for _, i := range m.Services { + _, exists := find.Slice(IDs, i.ID) + if exists { + duplicates = append(duplicates, i.ID) + } else { + IDs = append(IDs, i.ID) + } + } + + for _, i := range m.Relationships { + _, exists := find.Slice(IDs, i.ID) + if exists { + duplicates = append(duplicates, i.ID) + } else { + IDs = append(IDs, i.ID) + } + } + + return +} + +// ServiceExists loops through each Relationship and checks if the services are declared. +func (m *Config) ServiceExists() (missing map[string][]string) { + missing = make(map[string][]string) + IDs := []string{} + + for _, i := range m.Services { + IDs = append(IDs, i.ID) + } + + for _, i := range m.Relationships { + _, exists := find.Slice(IDs, i.To) + if !exists { + missing[i.ID] = append(missing[i.ID], i.To) + } + + _, exists = find.Slice(IDs, i.From) + if !exists { + missing[i.ID] = append(missing[i.ID], i.From) + } + } + + return +} diff --git a/internal/platform/constellation/find_test.go b/internal/platform/constellation/find_test.go index 8bda99b..62eb3c0 100644 --- a/internal/platform/constellation/find_test.go +++ b/internal/platform/constellation/find_test.go @@ -34,3 +34,122 @@ func TestMultipleInstanceInRelationships(t *testing.T) { assert.EqualValues(t, 2, len(from), "Event Generator did not have the correct number of `From` relationships") assert.EqualValues(t, 2, len(to), "Event Logger did not have the correct number of `To` relationships") } + +func TestForDuplicatIDsInServices(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/valid.yaml") + assert.NoError(t, err) + + duplicates := testData.FindDuplicateIDs() + assert.Nil(t, duplicates, "No duplicates should be found.") +} + +func TestServicesExists(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/valid.yaml") + assert.NoError(t, err) + + missing := testData.ServiceExists() + assert.Empty(t, missing, "No missing services should be found.") +} + +func TestForDuplicatIDsInServicesFail(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/duplicate/servIds.yaml") + assert.NoError(t, err) + + duplicates := testData.FindDuplicateIDs() + + assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 2) + assert.Equal(t, 2, len(duplicates)) +} + +func TestForDuplicatIDsInRelationshipsFail(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/duplicate/relIds.yaml") + assert.NoError(t, err) + + duplicates := testData.FindDuplicateIDs() + + assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 1) + assert.Equal(t, 1, len(duplicates)) +} + +func TestForDuplicatIDsFail(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/duplicate/servRelIds.yaml") + assert.NoError(t, err) + + duplicates := testData.FindDuplicateIDs() + + assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 1) + assert.Equal(t, 1, len(duplicates)) +} + +func TestServicesExistsFail(t *testing.T) { + expected := "Azure Event Hub" + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/missing/relServRefId.yaml") + assert.NoError(t, err) + + missing := testData.ServiceExists() + foundID := missing["Event Hubs to Event Logger Link"][0] + + assert.Equal(t, 1, len(missing), "There should be only 1 missing services found") + assert.Equalf(t, expected, foundID, "Incorrect reference found\nExpected: %v \nGot: %v", expected, foundID) +} + +func TestSchemaMissingDagName(t *testing.T) { + testData := new(constellation.Config) + err := testData.LoadFile("testdata/missing/name.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should be invalid") +} + +func TestSchemaMissingDagID(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/missing/id.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should be invalid") +} + +func TestSchemaMissingService(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/missing/serv.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should be invalid") +} + +func TestSchemaMissingServiceID(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/missing/servId.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should be invalid") +} + +func TestSchemaMissingRelationshipID(t *testing.T) { + testData := new(constellation.Config) + + err := testData.LoadFile("testdata/missing/relId.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should be invalid") +} \ No newline at end of file diff --git a/internal/platform/constellation/validate.go b/internal/platform/constellation/validate.go deleted file mode 100644 index 485cee9..0000000 --- a/internal/platform/constellation/validate.go +++ /dev/null @@ -1,60 +0,0 @@ -package constellation - -import ( - "github.com/microsoft/abstrakt/tools/find" - "gopkg.in/dealancer/validate.v2" -) - -// DuplicateIDs checks for duplicate Relationship and Service IDs in a constellation file. -func (m *Config) DuplicateIDs() (duplicates []string) { - IDs := []string{string(m.ID)} - - for _, i := range m.Services { - _, exists := find.Slice(IDs, i.ID) - if exists { - duplicates = append(duplicates, i.ID) - } else { - IDs = append(IDs, i.ID) - } - } - - for _, i := range m.Relationships { - _, exists := find.Slice(IDs, i.ID) - if exists { - duplicates = append(duplicates, i.ID) - } else { - IDs = append(IDs, i.ID) - } - } - - return -} - -// CheckServiceExists loops through each Relationship and checks if the services are declared. -func (m *Config) CheckServiceExists() (missing map[string][]string) { - missing = make(map[string][]string) - IDs := []string{} - - for _, i := range m.Services { - IDs = append(IDs, i.ID) - } - - for _, i := range m.Relationships { - _, exists := find.Slice(IDs, i.To) - if !exists { - missing[i.ID] = append(missing[i.ID], i.To) - } - - _, exists = find.Slice(IDs, i.From) - if !exists { - missing[i.ID] = append(missing[i.ID], i.From) - } - } - - return -} - -// ValidateModel checks if constellation has all required felids -func (m *Config) ValidateModel() error { - return validate.Validate(m) -} diff --git a/internal/platform/constellation/validate_test.go b/internal/platform/constellation/validate_test.go deleted file mode 100644 index 0d5f950..0000000 --- a/internal/platform/constellation/validate_test.go +++ /dev/null @@ -1,136 +0,0 @@ -package constellation_test - -import ( - "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/stretchr/testify/assert" - "testing" -) - -func TestForDuplicatIDsInServices(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/valid.yaml") - assert.NoError(t, err) - - duplicates := testData.DuplicateIDs() - assert.Nil(t, duplicates, "No duplicates should be found.") -} - -func TestServicesExists(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/valid.yaml") - assert.NoError(t, err) - - missing := testData.CheckServiceExists() - assert.Empty(t, missing, "No missing services should be found.") -} - -func TestSchemaChecks(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/valid.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.NoError(t, err, "Model validation should not return errors") -} - -func TestForDuplicatIDsInServicesFail(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/duplicate/servIds.yaml") - assert.NoError(t, err) - - duplicates := testData.DuplicateIDs() - - assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 2) - assert.Equal(t, 2, len(duplicates)) -} - -func TestForDuplicatIDsInRelationshipsFail(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/duplicate/relIds.yaml") - assert.NoError(t, err) - - duplicates := testData.DuplicateIDs() - - assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 1) - assert.Equal(t, 1, len(duplicates)) -} - -func TestForDuplicatIDsFail(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/duplicate/servRelIds.yaml") - assert.NoError(t, err) - - duplicates := testData.DuplicateIDs() - - assert.NotNilf(t, duplicates, "There should be %v duplicate IDs found", 1) - assert.Equal(t, 1, len(duplicates)) -} - -func TestServicesExistsFail(t *testing.T) { - expected := "Azure Event Hub" - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/missing/relServRefId.yaml") - assert.NoError(t, err) - - missing := testData.CheckServiceExists() - foundID := missing["Event Hubs to Event Logger Link"][0] - - assert.Equal(t, 1, len(missing), "There should be only 1 missing services found") - assert.Equalf(t, expected, foundID, "Incorrect reference found\nExpected: %v \nGot: %v", expected, foundID) -} - -func TestSchemaMissingDagName(t *testing.T) { - testData := new(constellation.Config) - err := testData.LoadFile("testdata/missing/name.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.Error(t, err, "Model validation should be invalid") -} - -func TestSchemaMissingDagID(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/missing/id.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.Error(t, err, "Model validation should be invalid") -} - -func TestSchemaMissingService(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/missing/serv.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.Error(t, err, "Model validation should be invalid") -} - -func TestSchemaMissingServiceID(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/missing/servId.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.Error(t, err, "Model validation should be invalid") -} - -func TestSchemaMissingRelationshipID(t *testing.T) { - testData := new(constellation.Config) - - err := testData.LoadFile("testdata/missing/relId.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.Error(t, err, "Model validation should be invalid") -} diff --git a/internal/platform/mapper/find.go b/internal/platform/mapper/find.go index 1f4bd20..aa5ac64 100644 --- a/internal/platform/mapper/find.go +++ b/internal/platform/mapper/find.go @@ -1,6 +1,7 @@ package mapper import ( + "github.com/microsoft/abstrakt/tools/find" "github.com/microsoft/abstrakt/tools/guid" "strings" ) @@ -30,3 +31,51 @@ func (m *Config) FindByType(typeName string) *Info { } return nil } + +// FindDuplicateChartName checks for duplicate chart names in a mapper file. +func (m *Config) FindDuplicateChartName() (duplicates []string) { + chartNames := []string{} + + for _, i := range m.Maps { + _, exists := find.Slice(chartNames, i.ChartName) + if exists { + duplicates = append(duplicates, i.ChartName) + } else { + chartNames = append(chartNames, i.ChartName) + } + } + + return +} + +// FindDuplicateType checks for duplicate types in a mapper file. +func (m *Config) FindDuplicateType() (duplicates []string) { + types := []string{} + + for _, i := range m.Maps { + _, exists := find.Slice(types, i.Type) + if exists { + duplicates = append(duplicates, i.Type) + } else { + types = append(types, i.Type) + } + } + + return +} + +// FindDuplicateLocation checks for duplicate location in a mapper file. +func (m *Config) FindDuplicateLocation() (duplicates []string) { + location := []string{} + + for _, i := range m.Maps { + _, exists := find.Slice(location, i.Location) + if exists { + duplicates = append(duplicates, i.Location) + } else { + location = append(location, i.Location) + } + } + + return +} diff --git a/internal/platform/mapper/find_test.go b/internal/platform/mapper/find_test.go index ec2b167..e765759 100644 --- a/internal/platform/mapper/find_test.go +++ b/internal/platform/mapper/find_test.go @@ -67,3 +67,114 @@ func TestFindByType(t *testing.T) { assert.Equal(t, "../../helm/basictest3", info.Location) assert.Equal(t, "1.0.0", info.Version) } + +func TestDuplicateChartNamePass(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + duplicate := testData.FindDuplicateChartName() + assert.Nil(t, duplicate) +} + +func TestDuplicateTypesPass(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + duplicate := testData.FindDuplicateType() + assert.Nil(t, duplicate) +} + +func TestDuplicateLocationsPass(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + duplicate := testData.FindDuplicateLocation() + assert.Nil(t, duplicate) +} + +func TestSchemaFailMissingChartNameValue(t *testing.T) { + expected := "Validation error in field \"ChartName\" of type \"string\" using validator \"empty=false\"" + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/missing/chartNameValue.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should return error") + assert.EqualError(t, err, expected, "Model validation should return error") +} + +func TestSchemaFailMissingVersionProperty(t *testing.T) { + expected := "Validation error in field \"Version\" of type \"string\" using validator \"empty=false\"" + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/missing/versionProperty.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should return error") + assert.EqualError(t, err, expected, "Model validation should return error") +} + +func TestSchemaFailMissingMap(t *testing.T) { + expected := "Validation error in field \"Maps\" of type \"[]mapper.Info\" using validator \"empty=false\"" + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/missing/map.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.Error(t, err, "Model validation should return error") + assert.EqualError(t, err, expected, "Model validation should return error") +} + +func TestDuplicateChartName(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/duplicate/chartNames.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.NoError(t, err, "Model validation should not return error") + + duplicate := testData.FindDuplicateChartName() + assert.NotNil(t, duplicate) + assert.Equal(t, 2, len(duplicate)) + assert.Equal(t, "event_hub_sample_event_generator", duplicate[0]) +} + +func TestDuplicateTypes(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/duplicate/types.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.NoError(t, err, "Model validation should not return error") + + duplicate := testData.FindDuplicateType() + assert.NotNil(t, duplicate) + assert.Equal(t, 2, len(duplicate)) + assert.Equal(t, "EventHub", duplicate[0]) +} + +func TestDuplicateLocation(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/duplicate/locations.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.NoError(t, err, "Model validation should not return error") + + duplicate := testData.FindDuplicateLocation() + assert.NotNil(t, duplicate) + assert.Equal(t, 2, len(duplicate)) + assert.Equal(t, "../../helm/basictest", duplicate[0]) +} diff --git a/internal/platform/mapper/mapper.go b/internal/platform/mapper/mapper.go index 5ac07b8..a662c16 100644 --- a/internal/platform/mapper/mapper.go +++ b/internal/platform/mapper/mapper.go @@ -11,6 +11,7 @@ import ( yamlParser "gopkg.in/yaml.v2" "io/ioutil" "reflect" + "gopkg.in/dealancer/validate.v2" ) // Note: the yaml mapping attributes are necessary (despite the nearly @@ -51,3 +52,8 @@ func (m *Config) LoadString(yamlString string) error { func (m *Config) IsEmpty() bool { return reflect.DeepEqual(Config{}, *m) } + +// ValidateModel checks if mapper has all required felids +func (m *Config) ValidateModel() error { + return validate.Validate(m) +} diff --git a/internal/platform/mapper/mapper_test.go b/internal/platform/mapper/mapper_test.go index 7faa3fc..e0160e8 100644 --- a/internal/platform/mapper/mapper_test.go +++ b/internal/platform/mapper/mapper_test.go @@ -63,6 +63,16 @@ func TestIsEmptyFalse(t *testing.T) { assert.False(t, dag.IsEmpty()) } +func TestSchemaPass(t *testing.T) { + testData := new(mapper.Config) + + err := testData.LoadFile("testdata/mapper.yaml") + assert.NoError(t, err) + + err = testData.ValidateModel() + assert.NoError(t, err) +} + var buildMap01 = mapper.Config{ Name: "Basic Azure Event Hubs maps", ID: guid.GUID("a5a7c413-a020-44a2-bd23-1941adb7ad58"), diff --git a/internal/platform/mapper/validate.go b/internal/platform/mapper/validate.go deleted file mode 100644 index 213eba7..0000000 --- a/internal/platform/mapper/validate.go +++ /dev/null @@ -1,59 +0,0 @@ -package mapper - -import ( - "github.com/microsoft/abstrakt/tools/find" - "gopkg.in/dealancer/validate.v2" -) - -// DuplicateChartName checks for duplicate chart names in a mapper file. -func (m *Config) DuplicateChartName() (duplicates []string) { - chartNames := []string{} - - for _, i := range m.Maps { - _, exists := find.Slice(chartNames, i.ChartName) - if exists { - duplicates = append(duplicates, i.ChartName) - } else { - chartNames = append(chartNames, i.ChartName) - } - } - - return -} - -// DuplicateType checks for duplicate types in a mapper file. -func (m *Config) DuplicateType() (duplicates []string) { - types := []string{} - - for _, i := range m.Maps { - _, exists := find.Slice(types, i.Type) - if exists { - duplicates = append(duplicates, i.Type) - } else { - types = append(types, i.Type) - } - } - - return -} - -// DuplicateLocation checks for duplicate location in a mapper file. -func (m *Config) DuplicateLocation() (duplicates []string) { - location := []string{} - - for _, i := range m.Maps { - _, exists := find.Slice(location, i.Location) - if exists { - duplicates = append(duplicates, i.Location) - } else { - location = append(location, i.Location) - } - } - - return -} - -// ValidateModel checks if mapper has all required felids -func (m *Config) ValidateModel() error { - return validate.Validate(m) -} diff --git a/internal/platform/mapper/validate_test.go b/internal/platform/mapper/validate_test.go deleted file mode 100644 index 19dbbd0..0000000 --- a/internal/platform/mapper/validate_test.go +++ /dev/null @@ -1,128 +0,0 @@ -package mapper_test - -import ( - "github.com/microsoft/abstrakt/internal/platform/mapper" - "github.com/stretchr/testify/assert" - "testing" -) - -func TestSchemaPass(t *testing.T) { - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/mapper.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.NoError(t, err) -} - -func TestDuplicateChartNamePass(t *testing.T) { - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/mapper.yaml") - assert.NoError(t, err) - - duplicate := testData.DuplicateChartName() - assert.Nil(t, duplicate) -} - -func TestDuplicateTypesPass(t *testing.T) { - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/mapper.yaml") - assert.NoError(t, err) - - duplicate := testData.DuplicateType() - assert.Nil(t, duplicate) -} - -func TestDuplicateLocationsPass(t *testing.T) { - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/mapper.yaml") - assert.NoError(t, err) - - duplicate := testData.DuplicateLocation() - assert.Nil(t, duplicate) -} - -func TestSchemaFailMissingChartNameValue(t *testing.T) { - expected := "Validation error in field \"ChartName\" of type \"string\" using validator \"empty=false\"" - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/missing/chartNameValue.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.Error(t, err, "Model validation should return error") - assert.EqualError(t, err, expected, "Model validation should return error") -} - -func TestSchemaFailMissingVersionProperty(t *testing.T) { - expected := "Validation error in field \"Version\" of type \"string\" using validator \"empty=false\"" - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/missing/versionProperty.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.Error(t, err, "Model validation should return error") - assert.EqualError(t, err, expected, "Model validation should return error") -} - -func TestSchemaFailMissingMap(t *testing.T) { - expected := "Validation error in field \"Maps\" of type \"[]mapper.Info\" using validator \"empty=false\"" - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/missing/map.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.Error(t, err, "Model validation should return error") - assert.EqualError(t, err, expected, "Model validation should return error") -} - -func TestDuplicateChartName(t *testing.T) { - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/duplicate/chartNames.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.NoError(t, err, "Model validation should not return error") - - duplicate := testData.DuplicateChartName() - assert.NotNil(t, duplicate) - assert.Equal(t, 2, len(duplicate)) - assert.Equal(t, "event_hub_sample_event_generator", duplicate[0]) -} - -func TestDuplicateTypes(t *testing.T) { - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/duplicate/types.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.NoError(t, err, "Model validation should not return error") - - duplicate := testData.DuplicateType() - assert.NotNil(t, duplicate) - assert.Equal(t, 2, len(duplicate)) - assert.Equal(t, "EventHub", duplicate[0]) -} - -func TestDuplicateLocation(t *testing.T) { - testData := new(mapper.Config) - - err := testData.LoadFile("testdata/duplicate/locations.yaml") - assert.NoError(t, err) - - err = testData.ValidateModel() - assert.NoError(t, err, "Model validation should not return error") - - duplicate := testData.DuplicateLocation() - assert.NotNil(t, duplicate) - assert.Equal(t, 2, len(duplicate)) - assert.Equal(t, "../../helm/basictest", duplicate[0]) -} From 576f7cca0b435f77ba262e152425d272207a836c Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 29 Jan 2020 10:53:51 +1100 Subject: [PATCH 55/85] Fixed unit test. --- cmd/validate.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/validate.go b/cmd/validate.go index 9bf6368..6466a9a 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -133,7 +133,7 @@ func validateDag(d *constellation.Config) (err error) { } logger.Debug("constellation: checking for duplicate `ID`") - duplicates := d.DuplicateIDs() + duplicates := d.FindDuplicateIDs() if duplicates != nil { logger.Error("Duplicate `ID` present in config") @@ -144,7 +144,7 @@ func validateDag(d *constellation.Config) (err error) { } logger.Debug("Constellation: checking if `Service` exists") - connections := d.CheckServiceExists() + connections := d.ServiceExists() if len(connections) > 0 { logger.Error("Missing relationship(s)") @@ -181,7 +181,7 @@ func validateMapper(m *mapper.Config) (err error) { } logger.Debug("Mapper: checking for duplicate `ChartName`") - duplicates := m.DuplicateChartName() + duplicates := m.FindDuplicateChartName() if duplicates != nil { logger.Error("Duplicate `ChartName` present in config") @@ -192,7 +192,7 @@ func validateMapper(m *mapper.Config) (err error) { } logger.Debug("Mapper: checking for duplicate `Type`") - duplicates = m.DuplicateType() + duplicates = m.FindDuplicateType() if duplicates != nil { logger.Error("Duplicate `Type` present in config") @@ -203,7 +203,7 @@ func validateMapper(m *mapper.Config) (err error) { } logger.Debug("Mapper: checking for duplicate `Location`") - duplicates = m.DuplicateLocation() + duplicates = m.FindDuplicateLocation() if duplicates != nil { logger.Error("Duplicate `Location` present in config") From 72f75c5f6e694f7487269a6610b4763f9029a1a5 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 29 Jan 2020 10:59:56 +1100 Subject: [PATCH 56/85] Fixed linting issue. --- internal/platform/constellation/find_test.go | 2 +- internal/platform/mapper/mapper.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/platform/constellation/find_test.go b/internal/platform/constellation/find_test.go index 62eb3c0..0aebc9f 100644 --- a/internal/platform/constellation/find_test.go +++ b/internal/platform/constellation/find_test.go @@ -152,4 +152,4 @@ func TestSchemaMissingRelationshipID(t *testing.T) { err = testData.ValidateModel() assert.Error(t, err, "Model validation should be invalid") -} \ No newline at end of file +} diff --git a/internal/platform/mapper/mapper.go b/internal/platform/mapper/mapper.go index a662c16..c26c3ba 100644 --- a/internal/platform/mapper/mapper.go +++ b/internal/platform/mapper/mapper.go @@ -8,10 +8,10 @@ package mapper import ( "github.com/microsoft/abstrakt/tools/guid" + "gopkg.in/dealancer/validate.v2" yamlParser "gopkg.in/yaml.v2" "io/ioutil" "reflect" - "gopkg.in/dealancer/validate.v2" ) // Note: the yaml mapping attributes are necessary (despite the nearly From 9528c5565c15309ae663357577cb9246e8ae0732 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 12:21:09 +1100 Subject: [PATCH 57/85] Standardised test helper. --- cmd/compose_test.go | 32 +++++++++---------- internal/diff/diff_test.go | 4 +-- internal/platform/constellation/graph_test.go | 4 +-- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/cmd/compose_test.go b/cmd/compose_test.go index 819961a..b3ab0c4 100644 --- a/cmd/compose_test.go +++ b/cmd/compose_test.go @@ -1,45 +1,45 @@ package cmd import ( - "github.com/microsoft/abstrakt/tools/test" + helper "github.com/microsoft/abstrakt/tools/test" "github.com/stretchr/testify/assert" "testing" ) func TestComposeCommandReturnsErrorIfTemplateTypeIsInvalid(t *testing.T) { templateType := "ble" - constellationPath, mapsPath, tdir := test.PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := helper.PrepareRealFilesForTest(t) - defer test.CleanTempTestFiles(t, tdir) + defer helper.CleanTempTestFiles(t, tdir) - output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-if-template-type-is-invalid", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + output, err := helper.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-if-template-type-is-invalid", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) assert.Errorf(t, err, "Did not received expected error. \nGot:\n %v", output) } func TestComposeCommandDoesNotErrorIfTemplateTypeIsEmptyOrHelm(t *testing.T) { templateType := "" - constellationPath, mapsPath, tdir := test.PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := helper.PrepareRealFilesForTest(t) - defer test.CleanTempTestFiles(t, tdir) + defer helper.CleanTempTestFiles(t, tdir) - output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + output, err := helper.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) assert.NoErrorf(t, err, "Did not expect error:\n %v\n output: %v", err, output) templateType = "helm" - output, err = test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) + output, err = helper.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-does-not-error-if-template-type-is-empty-or-helm", "-f", constellationPath, "-m", mapsPath, "-t", templateType, "-o", tdir) assert.NoErrorf(t, err, "Did not expect error:\n %v\n output: %v", err, output) } func TestComposeCommandReturnsErrorWithInvalidFilePaths(t *testing.T) { - output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-with-invalid-files", "-f", "invalid", "-m", "invalid", "-o", "invalid") + output, err := helper.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-returns-error-with-invalid-files", "-f", "invalid", "-m", "invalid", "-o", "invalid") assert.Errorf(t, err, "Did not received expected error. \nGot:\n %v", output) } func TestComposeCmdVerifyRequiredFlags(t *testing.T) { expected := "required flag(s) \"constellationFilePath\", \"mapsFilePath\", \"outputPath\" not set" - output, err := test.ExecuteCommand(newComposeCmd().cmd, "") + output, err := helper.ExecuteCommand(newComposeCmd().cmd, "") if err != nil { assert.Contains(t, err.Error(), expected) @@ -49,19 +49,19 @@ func TestComposeCmdVerifyRequiredFlags(t *testing.T) { } func TestComposeCmdWithValidFlags(t *testing.T) { - constellationPath, mapsPath, tdir := test.PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := helper.PrepareRealFilesForTest(t) - defer test.CleanTempTestFiles(t, tdir) + defer helper.CleanTempTestFiles(t, tdir) - output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-flags", "-f", constellationPath, "-m", mapsPath, "-o", tdir) + output, err := helper.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-flags", "-f", constellationPath, "-m", mapsPath, "-o", tdir) assert.NoErrorf(t, err, "error: \n %v\noutput:\n %v\n", err, output) } func TestComposeWithRealFiles(t *testing.T) { - constellationPath, mapsPath, tdir := test.PrepareRealFilesForTest(t) + constellationPath, mapsPath, tdir := helper.PrepareRealFilesForTest(t) - defer test.CleanTempTestFiles(t, tdir) + defer helper.CleanTempTestFiles(t, tdir) - output, err := test.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-real-files", "-f", constellationPath, "-m", mapsPath, "-o", tdir) + output, err := helper.ExecuteCommand(newComposeCmd().cmd, "test-compose-cmd-with-real-files", "-f", constellationPath, "-m", mapsPath, "-o", tdir) assert.NoErrorf(t, err, "error: \n %v\noutput:\n %v\n", err, output) } diff --git a/internal/diff/diff_test.go b/internal/diff/diff_test.go index d0837cf..2dc70db 100644 --- a/internal/diff/diff_test.go +++ b/internal/diff/diff_test.go @@ -4,7 +4,7 @@ import ( set "github.com/deckarep/golang-set" "github.com/microsoft/abstrakt/internal/diff" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/tools/test" + helper "github.com/microsoft/abstrakt/tools/test" "github.com/stretchr/testify/assert" "testing" ) @@ -58,7 +58,7 @@ func TestGraphWithChanges(t *testing.T) { resString, err := diff.CreateGraphWithChanges(dsGraphNew, &loadedSets) assert.NoError(t, err) - assert.Truef(t, test.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString), "Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) + assert.Truef(t, helper.CompareGraphOutputAsSets(testDiffComparisonOutputString, resString), "Resulting output does not match the reference comparison input \n RESULT \n%s EXPECTED \n%s", resString, testDiffComparisonOutputString) } // Utility to populate comparison sets with expected/known result diff --git a/internal/platform/constellation/graph_test.go b/internal/platform/constellation/graph_test.go index c3ec53b..16c89a8 100644 --- a/internal/platform/constellation/graph_test.go +++ b/internal/platform/constellation/graph_test.go @@ -3,7 +3,7 @@ package constellation_test import ( "bytes" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/tools/test" + helper "github.com/microsoft/abstrakt/tools/test" "github.com/stretchr/testify/assert" "testing" ) @@ -25,7 +25,7 @@ func TestGenerateGraph(t *testing.T) { retString, err := retConfig.GenerateGraph(out) assert.NoErrorf(t, err, "Should not receive error: %v", err) - assert.True(t, test.CompareGraphOutputAsSets(cmpString, retString), "Input graph did not generate expected output graphviz representation\nExpected:\n%v \nGot:\n%v", cmpString, retString) + assert.True(t, helper.CompareGraphOutputAsSets(cmpString, retString), "Input graph did not generate expected output graphviz representation\nExpected:\n%v \nGot:\n%v", cmpString, retString) } const test02ConstGraphString = `digraph Azure_Event_Hubs_Sample { From 62270e17723a86ae99eb5418dda371e0100b95d9 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 13:05:09 +1100 Subject: [PATCH 58/85] Bumping golangci-lint. --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index 1a5f0c5..4923283 100644 --- a/makefile +++ b/makefile @@ -15,7 +15,7 @@ lint-all: lint-prepare lint vet lint-prepare: ifeq (,$(shell which golangci-lint)) @echo "Installing golangci-lint" - curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.21.0 > /dev/null 2>&1 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1 > /dev/null 2>&1 else @echo "golangci-lint is installed" endif From 4ffcfe001749b964cdc5d11b9aa3538a8cf1f44e Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 13:07:59 +1100 Subject: [PATCH 59/85] Remove redirect. --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index 4923283..c2a2c9a 100644 --- a/makefile +++ b/makefile @@ -15,7 +15,7 @@ lint-all: lint-prepare lint vet lint-prepare: ifeq (,$(shell which golangci-lint)) @echo "Installing golangci-lint" - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1 > /dev/null 2>&1 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1 else @echo "golangci-lint is installed" endif From de4f06c3bc0be265a24a097e086184437967cc22 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 13:12:05 +1100 Subject: [PATCH 60/85] Typo in build pipeline. --- build/ci/azure-pipelines.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/ci/azure-pipelines.yaml b/build/ci/azure-pipelines.yaml index 9339589..3a1ef2f 100644 --- a/build/ci/azure-pipelines.yaml +++ b/build/ci/azure-pipelines.yaml @@ -39,14 +39,14 @@ steps: make lint-all displayName: 'Lint Abstrakt' workingDirectory: '$(System.DefaultWorkingDirectory)' - env: { GOPTH: '$(GOPATH)' } + env: { GOPATH: '$(GOPATH)' } failOnStderr: true - script: | make test-export-all displayName: 'Test Abstrakt' workingDirectory: '$(System.DefaultWorkingDirectory)' - env: { GOPTH: '$(GOPATH)' } + env: { GOPATH: '$(GOPATH)' } - task: PublishTestResults@2 inputs: From 7d90a5f39fbc054c5c261fa760d746d567b8903c Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 13:15:46 +1100 Subject: [PATCH 61/85] go env GOPATH -> GOPATH --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index c2a2c9a..7e58056 100644 --- a/makefile +++ b/makefile @@ -15,7 +15,7 @@ lint-all: lint-prepare lint vet lint-prepare: ifeq (,$(shell which golangci-lint)) @echo "Installing golangci-lint" - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.23.1 else @echo "golangci-lint is installed" endif From 2e00293e169376b1755077ec1a55e6653e3d8a24 Mon Sep 17 00:00:00 2001 From: JasonTheDeveloper Date: Fri, 31 Jan 2020 13:28:49 +1100 Subject: [PATCH 62/85] Updated goPath and goBin. --- build/ci/azure-pipelines.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/ci/azure-pipelines.yaml b/build/ci/azure-pipelines.yaml index 3a1ef2f..12d52ef 100644 --- a/build/ci/azure-pipelines.yaml +++ b/build/ci/azure-pipelines.yaml @@ -19,8 +19,8 @@ steps: displayName: 'Using Go v1.13' inputs: version: '1.13' - GOPATH: '$(GOPATH)' - GOBIN: '$(GOBIN)' + goPath: '$(GOPATH)' + goBin: '$(GOBIN)' - task: Go@0 displayName: 'Resolving Dependencies' From 8dfbfc3d7123b12a8837fba9c6cec481f88b08dc Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 13:51:32 +1100 Subject: [PATCH 63/85] Setting GOPATH. --- build/ci/azure-pipelines.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/build/ci/azure-pipelines.yaml b/build/ci/azure-pipelines.yaml index 12d52ef..9fc35c6 100644 --- a/build/ci/azure-pipelines.yaml +++ b/build/ci/azure-pipelines.yaml @@ -22,6 +22,15 @@ steps: goPath: '$(GOPATH)' goBin: '$(GOBIN)' +- task: Go@0 + inputs: + command: 'custom' + customCommand: 'env' + arguments: 'GOPATH | read ppath && ##vso[task.setvariable variable=gopath]$ppath' + +- bash: | + echo $GOPATH + - task: Go@0 displayName: 'Resolving Dependencies' inputs: From 530d34018fe4ad64729cd2ef48e8938f8cdbcba7 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 14:54:24 +1100 Subject: [PATCH 64/85] Echo'd GOPATH to PATH. --- build/ci/azure-pipelines.yaml | 13 ++++--------- makefile | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/build/ci/azure-pipelines.yaml b/build/ci/azure-pipelines.yaml index 9fc35c6..ec39b9a 100644 --- a/build/ci/azure-pipelines.yaml +++ b/build/ci/azure-pipelines.yaml @@ -16,20 +16,15 @@ variables: steps: - task: GoTool@0 - displayName: 'Using Go v1.13' + displayName: 'Using Go v1.13.1' inputs: - version: '1.13' + version: '1.13.1' goPath: '$(GOPATH)' goBin: '$(GOBIN)' -- task: Go@0 - inputs: - command: 'custom' - customCommand: 'env' - arguments: 'GOPATH | read ppath && ##vso[task.setvariable variable=gopath]$ppath' - - bash: | - echo $GOPATH + echo "##vso[task.setvariable variable=PATH]${PATH}:$(go env GOPATH)/bin" + displayName: 'Exporting GOPATH to PATH' - task: Go@0 displayName: 'Resolving Dependencies' diff --git a/makefile b/makefile index 7e58056..6615d9b 100644 --- a/makefile +++ b/makefile @@ -15,7 +15,7 @@ lint-all: lint-prepare lint vet lint-prepare: ifeq (,$(shell which golangci-lint)) @echo "Installing golangci-lint" - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.23.1 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.23.1 > /dev/null 2>&1 else @echo "golangci-lint is installed" endif From 09313b70677e307630f0703c4d2c4f8896898f89 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 15:01:25 +1100 Subject: [PATCH 65/85] Bumped golangci-lint in devcontainer. --- .devcontainer/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 7cc4608..5a7459a 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -44,7 +44,7 @@ RUN apt-get update \ github.com/mgechev/revive \ github.com/derekparker/delve/cmd/dlv 2>&1 \ && GO111MODULE=on go get golang.org/x/tools/gopls@latest \ - && curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.21.0 \ + && curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1 \ && apt-get autoremove -y \ && apt-get clean -y \ && rm -rf /var/lib/apt/lists/* From acc5d05d5b31509fcf6b48477da035e2b719b751 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 15:13:25 +1100 Subject: [PATCH 66/85] SilenceUsage and SilenceError in diff. --- cmd/diff.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/diff.go b/cmd/diff.go index 7ca2ec8..162c541 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -28,6 +28,8 @@ func newDiffCmd() *diffCmd { Long: `Diff is for producing a Graphviz dot notation representation of the difference between two constellations (line an old and new version) Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFilePathNew]`, + SilenceUsage: true, + SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) error { logger.Debug("args: " + strings.Join(args, " ")) From 1152880f56e6b7dee395f333a01a823e3a049363 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 31 Jan 2020 15:13:41 +1100 Subject: [PATCH 67/85] Removed redundant code. --- cmd/diff_test.go | 49 +++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/cmd/diff_test.go b/cmd/diff_test.go index 9963e05..c40c4d9 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -15,14 +15,8 @@ func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { hook := test.NewGlobal() _, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", constellationPathOrg, "-n", constellationPathNew) - if err != nil { - t.Error("Did not receive output") - } else { - assert.Truef(t, helper.CompareGraphOutputAsSets(testDiffComparisonOutputString, hook.LastEntry().Message), "Expcted output and produced output do not match : expected %s produced %s", testDiffComparisonOutputString, hook.LastEntry().Message) - // Did use this initially but wont work with the strongs output from the graphviz library as the sequence of entries in the output can change - // while the sequence may change the result is still valid and the same so am usinga local comparison function to get around this problem - // assert.Contains(t, hook.LastEntry().Message, testDiffComparisonOutputString) - } + assert.NoError(t, err) + assert.True(t, helper.CompareGraphOutputAsSets(testDiffComparisonOutputString, hook.LastEntry().Message)) } // TestDffCmdFailYaml - test diff command parameters @@ -30,46 +24,33 @@ func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { func TestDffCmdFailYaml(t *testing.T) { expected := "Could not open original YAML input file for reading constellationPathOrg" - output, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", "constellationPathOrg", "-n", "constellationPathNew") + _, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", "constellationPathOrg", "-n", "constellationPathNew") - if err != nil { - assert.Contains(t, err.Error(), expected) - } else { - t.Errorf("Did not fail and it should have. Expected: %v \nGot: %v", expected, output) - } + assert.Error(t, err) + assert.EqualError(t, err, expected) expected = "Could not open new YAML input file for reading constellationPathNew" - output, err = helper.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "constellationPathNew") - - if err != nil { - assert.Contains(t, err.Error(), expected) - } else { - t.Errorf("Did not fail and it should have. Expected: %v \nGot: %v", expected, output) - } + _, err = helper.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "constellationPathNew") + assert.Error(t, err) + assert.EqualError(t, err, expected) } // TestDiffCmdFailNotYaml - test diff command parameters // Test both required command line parameter files fail when provided with invalid input files (-o, -n) failing each in turn func TestDiffCmdFailNotYaml(t *testing.T) { - expected := "dagConfigService failed to load file" + expected := "dagConfigService failed to load file \"diff.go\": yaml: line 26: mapping values are not allowed in this context" - output, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", "diff.go", "-n", "diff.go") + _, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", "diff.go", "-n", "diff.go") - if err != nil { - assert.Contains(t, err.Error(), expected) - } else { - t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) - } + assert.Error(t, err) + assert.EqualError(t, err, expected) - output, err = helper.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "diff.go") + _, err = helper.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "diff.go") - if err != nil { - assert.Contains(t, err.Error(), expected) - } else { - t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) - } + assert.Error(t, err) + assert.EqualError(t, err, expected) } const testDiffComparisonOutputString = `digraph Azure_Event_Hubs_Sample_Changed_diff { From 38fc7dc5bafaf681c38d582f7c5ae05b7fd2e5e9 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 12:33:44 +1100 Subject: [PATCH 68/85] Removed if statement in validate_test.go. --- cmd/validate_test.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/cmd/validate_test.go b/cmd/validate_test.go index 9f77060..2d1aaf6 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -47,11 +47,8 @@ func TestValidateCommandConstellationFail(t *testing.T) { entries = append(entries, i.Message) } - if err != nil { - assert.Contains(t, entries, expected) - } else { - t.Errorf("Did not received expected error. \nExpected: %v\nGot:\n %v", expected, err.Error()) - } + assert.Error(t, err) + assert.Contains(t, entries, expected) } func TestValidateCommandMapFail(t *testing.T) { @@ -62,11 +59,8 @@ func TestValidateCommandMapFail(t *testing.T) { entries := helper.GetAllLogs(hook.AllEntries()) - if err != nil { - assert.Contains(t, entries, expected) - } else { - t.Errorf("Did not received expected error. \nExpected: %v\nGot:\n %v", expected, err.Error()) - } + assert.Error(t, err) + assert.Contains(t, entries, expected) } func TestValidateCommandConstellationInvalidSchema(t *testing.T) { From 924e6d65d068e09d015299b351d4a840cc9de2be Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 12:37:31 +1100 Subject: [PATCH 69/85] Removed if statement in version_test.go. --- cmd/version_test.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/cmd/version_test.go b/cmd/version_test.go index aba3406..78d1df2 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -26,9 +26,6 @@ func TestVersionCmd(t *testing.T) { hook := test.NewGlobal() _, err := helper.ExecuteCommand(newVersionCmd().cmd) - if err != nil { - t.Error(err) - } else { - assert.Contains(t, hook.LastEntry().Message, expected) - } + assert.NoError(t, err) + assert.Contains(t, hook.LastEntry().Message, expected) } From da5c02c08db50319525aa98ea77bd783b7310621 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 15:23:59 +1100 Subject: [PATCH 70/85] Removed if statement and panic from visualise_test.go. --- cmd/visualise_test.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/cmd/visualise_test.go b/cmd/visualise_test.go index b54c010..1215b1f 100644 --- a/cmd/visualise_test.go +++ b/cmd/visualise_test.go @@ -72,28 +72,23 @@ func TestFileExists(t *testing.T) { assert.False(t, result, "Test file does not exist but testFile says it does") err = os.Remove(testValidFilename) - if err != nil { - panic(err) - } + assert.NoError(t, err) result = file.Exists(testValidFilename) //Expecting false - file has been removed assert.False(t, result, "Test file has been removed but fileExists is finding it") } func TestParseYaml(t *testing.T) { - retConfig := new(constellation.Config) err := retConfig.LoadString(testValidYAMLString) - if err != nil { - panic(err) - } + assert.NoError(t, err) - if retConfig.Name != "Azure Event Hubs Sample" && - retConfig.ID != "d6e4a5e9-696a-4626-ba7a-534d6ff450a5" && - len(retConfig.Services) != 1 && - len(retConfig.Relationships) != 1 { - t.Errorf("YAML did not parse correctly and it should have") - } + errMsg := "YAML did not parse correctly and it should have" + + assert.Equalf(t, retConfig.Name, "Azure Event Hubs Sample", errMsg) + assert.EqualValuesf(t, retConfig.ID, "211a55bd-5d92-446c-8be8-190f8f0e623e", errMsg) + assert.Equalf(t, len(retConfig.Services), 1, errMsg) + assert.Equalf(t, len(retConfig.Relationships), 1, errMsg) } const testValidYAMLString = ` From 9c5d319623cc9062d78532b6f0ab42da3dc78b4e Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 15:57:27 +1100 Subject: [PATCH 71/85] Clean-up visualise_test.go. --- cmd/visualise_test.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/cmd/visualise_test.go b/cmd/visualise_test.go index 1215b1f..56b3f67 100644 --- a/cmd/visualise_test.go +++ b/cmd/visualise_test.go @@ -18,35 +18,26 @@ func TestVisualiseCmdWithAllRequirementsNoError(t *testing.T) { hook := test.NewGlobal() _, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", constellationPath) - if err != nil { - t.Error("Did not receive output") - } else { - assert.Truef(t, helper.CompareGraphOutputAsSets(validGraphString, hook.LastEntry().Message), "Expcted output and produced output do not match : expected %s produced %s", validGraphString, hook.LastEntry().Message) - } + assert.NoError(t, err) + assert.True(t, helper.CompareGraphOutputAsSets(validGraphString, hook.LastEntry().Message)) } func TestVisualiseCmdFailYaml(t *testing.T) { expected := "Could not open YAML input file for reading" - output, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", "constellationPath") + _, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", "constellationPath") - if err != nil { - assert.Contains(t, err.Error(), expected) - } else { - t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) - } + assert.Error(t, err) + assert.Contains(t, err.Error(), expected) } func TestVisualiseCmdFailNotYaml(t *testing.T) { expected := "dagConfigService failed to load file" - output, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", "visualise.go") + _, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", "visualise.go") - if err != nil { - assert.Contains(t, err.Error(), expected) - } else { - t.Errorf("Did not fail. Expected: %v \nGot: %v", expected, output) - } + assert.Error(t, err) + assert.Contains(t, err.Error(), expected) } func TestFileExists(t *testing.T) { From 56583b83af2d68f98aadc15f4febff5dc671ab5c Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 16:30:40 +1100 Subject: [PATCH 72/85] Added value testdata. --- internal/compose/testdata/values.yaml | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 internal/compose/testdata/values.yaml diff --git a/internal/compose/testdata/values.yaml b/internal/compose/testdata/values.yaml new file mode 100644 index 0000000..e7dc246 --- /dev/null +++ b/internal/compose/testdata/values.yaml @@ -0,0 +1,39 @@ +event_hub_sample_event_generator: + name: event_hub_sample_event_generator + relationships: + output: + - name: event_hub_sample_event_hub + service: 211a55bd-5d92-446c-8be8-190f8f0e623e + type: EventHub + type: EventGenerator +event_hub_sample_event_hub: + name: event_hub_sample_event_hub + relationships: + input: + - name: event_hub_sample_event_generator + service: 211a55bd-5d92-446c-8be8-190f8f0e623e + type: EventGenerator + output: + - name: event_hub_sample_event_logger1 + service: 08ccbd67-456f-4349-854a-4e6959e5017b + type: EventLogger + - name: event_hub_sample_event_logger + service: c8a719e0-164d-408f-9ed1-06e08dc5abbe + type: EventLogger + type: EventHub +event_hub_sample_event_logger: + name: event_hub_sample_event_logger + relationships: + input: + - name: event_hub_sample_event_hub + service: c8a719e0-164d-408f-9ed1-06e08dc5abbe + type: EventHub + type: EventLogger +event_hub_sample_event_logger1: + name: event_hub_sample_event_logger1 + relationships: + input: + - name: event_hub_sample_event_hub + service: 08ccbd67-456f-4349-854a-4e6959e5017b + type: EventHub + type: EventLogger From 9e93aeabd6648da7b844b37cdf05274668ce06f1 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 16:31:02 +1100 Subject: [PATCH 73/85] Added asserts. --- internal/compose/compose_test.go | 47 ++++++++++---------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go index 0705072..81a4a1c 100644 --- a/internal/compose/compose_test.go +++ b/internal/compose/compose_test.go @@ -1,8 +1,8 @@ package compose_test import ( - "fmt" "github.com/microsoft/abstrakt/internal/compose" + helper "github.com/microsoft/abstrakt/tools/test" "github.com/stretchr/testify/assert" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" @@ -14,20 +14,12 @@ import ( ) func TestComposeService(t *testing.T) { + _, _, tdir := helper.PrepareRealFilesForTest(t) - tdir, err := ioutil.TempDir("", "helm-") - if err != nil { - t.Fatal(err) - } - defer func() { - err = os.RemoveAll(tdir) - if err != nil { - t.Fatal(err) - } - }() + defer helper.CleanTempTestFiles(t, tdir) comp := new(compose.Composer) - _, err = comp.Build("test", tdir) + _, err := comp.Build("test", tdir) assert.Error(t, err, "Compose should fail if not yet loaded") @@ -42,37 +34,29 @@ func TestComposeService(t *testing.T) { _ = chartutil.SaveDir(h, tdir) h, _ = loader.LoadDir(tdir) + + contentBytes, err := ioutil.ReadFile("testdata/values.yaml") + assert.NoError(t, err) + for _, raw := range h.Raw { if raw.Name == "test/values.yaml" { - fmt.Print(string(raw.Data)) + assert.Equal(t, string(contentBytes), string(raw.Data)) } } } func TestHelmLibCompose(t *testing.T) { + _, _, tdir := helper.PrepareRealFilesForTest(t) - tdir, err := ioutil.TempDir("", "helm-") - if err != nil { - t.Fatal(err) - } - defer func() { - err = os.RemoveAll(tdir) - if err != nil { - t.Fatal(err) - } - }() + defer helper.CleanTempTestFiles(t, tdir) c, err := chartutil.Create("foo", tdir) - if err != nil { - t.Fatal(err) - } + assert.NoError(t, err) dir := filepath.Join(tdir, "foo") mychart, err := loader.LoadDir(c) - if err != nil { - t.Fatalf("Failed to load newly created chart %q: %s", c, err) - } + assert.NoErrorf(t, err, "Failed to load newly created chart %q: %s", c, err) assert.Equalf(t, "foo", mychart.Name(), "Expected name to be 'foo', got %q", mychart.Name()) @@ -89,9 +73,8 @@ func TestHelmLibCompose(t *testing.T) { chartutil.TestConnectionName, chartutil.ValuesfileName, } { - if _, err := os.Stat(filepath.Join(dir, f)); err != nil { - t.Errorf("Expected %s file: %s", f, err) - } + _, err := os.Stat(filepath.Join(dir, f)) + assert.NoError(t, err) } mychart.Values["Jordan"] = "testing123" From 17ce86b762322d6e720b52203bd3268c93316274 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 16:32:33 +1100 Subject: [PATCH 74/85] Added assert in diff_test.go. --- internal/diff/diff_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/internal/diff/diff_test.go b/internal/diff/diff_test.go index 2dc70db..b5d8527 100644 --- a/internal/diff/diff_test.go +++ b/internal/diff/diff_test.go @@ -38,14 +38,12 @@ func TestGetComparisonSets(t *testing.T) { assert.True(t, knownSets.SetDelRels.Equal(loadedSets.SetDelRels), "Deleted relationships - did not match between expected result and input yaml") } -// testGraphWithChanges - test diff comparison function +// TestGraphWithChanges - test diff comparison function func TestGraphWithChanges(t *testing.T) { dsGraphOrg := new(constellation.Config) err := dsGraphOrg.LoadFile("testdata/original.yaml") - if err != nil { - t.Errorf("dagConfigService failed to load dag from test string %s", err) - } + assert.NoError(t, err) dsGraphNew := new(constellation.Config) err = dsGraphNew.LoadFile("testdata/modified.yaml") From 7755ce1dc3ba3eb7c6f948b461427a5649fb35d7 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 16:47:58 +1100 Subject: [PATCH 75/85] Fail test in compose_test.go. --- internal/compose/compose_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go index 81a4a1c..8cae522 100644 --- a/internal/compose/compose_test.go +++ b/internal/compose/compose_test.go @@ -51,12 +51,16 @@ func TestHelmLibCompose(t *testing.T) { defer helper.CleanTempTestFiles(t, tdir) c, err := chartutil.Create("foo", tdir) - assert.NoError(t, err) + if err != nil { + assert.FailNow(t, err.Error()) + } dir := filepath.Join(tdir, "foo") mychart, err := loader.LoadDir(c) - assert.NoErrorf(t, err, "Failed to load newly created chart %q: %s", c, err) + if err != nil { + assert.FailNowf(t, "Failed to load newly created chart %q: %s", c, err) + } assert.Equalf(t, "foo", mychart.Name(), "Expected name to be 'foo', got %q", mychart.Name()) From fd454ded59b598e6a0dd4539dd36ce46eaa1b976 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 17:26:42 +1100 Subject: [PATCH 76/85] assert in chart_test.go. --- internal/platform/chart/chart_test.go | 64 +++++++++++++-------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/internal/platform/chart/chart_test.go b/internal/platform/chart/chart_test.go index ba78835..c153021 100644 --- a/internal/platform/chart/chart_test.go +++ b/internal/platform/chart/chart_test.go @@ -7,6 +7,7 @@ import ( "compress/gzip" "flag" "github.com/microsoft/abstrakt/internal/platform/chart" + "github.com/stretchr/testify/assert" "io" "io/ioutil" "os" @@ -26,38 +27,38 @@ func TestUpdate(t *testing.T) { err := os.RemoveAll("testdata/golden") if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } err = os.MkdirAll("testdata/golden/helm/charts", os.ModePerm) if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } err = exec.Command("cp", "-r", "testdata/sample/helm", "testdata/golden/").Run() if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } // Create zipped dependant charts err = exec.Command("tar", "cfz", "testdata/golden/helm/charts/event_hub_sample_event_generator-1.0.0.tgz", "testdata/sample/deps/event_hub_sample_event_generator/").Run() if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } err = exec.Command("tar", "cfz", "testdata/golden/helm/charts/event_hub_sample_event_hub-1.0.0.tgz", "testdata/sample/deps/event_hub_sample_event_hub/").Run() if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } err = exec.Command("tar", "cfz", "testdata/golden/helm/charts/event_hub_sample_event_logger-1.0.0.tgz", "testdata/sample/deps/event_hub_sample_event_logger/").Run() if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } err = exec.Command("tar", "cfz", "testdata/golden/test-0.1.0.tgz", "testdata/sample/helm").Run() if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } } @@ -66,33 +67,30 @@ func TestChartSavesAndLoads(t *testing.T) { tdir2, err2 := ioutil.TempDir("./", "output-") if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } if err2 != nil { - t.Fatal(err2) + assert.FailNow(t, err2.Error()) } + defer func() { err = os.RemoveAll(tdir) - if err != nil { - t.Error(err) - } - err = os.RemoveAll(tdir2) - if err != nil { - t.Error(err) - } + assert.NoError(t, err) + err = os.RemoveAll(tdir2) + assert.NoError(t, err) }() c, err := chart.Create("foo", tdir) if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } err = chart.SaveToDir(c, tdir2) if err != nil { - t.Fatalf("Failed to save newly created chart %q: %s", tdir2, err) + assert.FailNowf(t, "Failed to save newly created chart %q: %s", tdir2, err) } newPath := filepath.Join(tdir2, "foo") @@ -100,36 +98,36 @@ func TestChartSavesAndLoads(t *testing.T) { _, err = chart.LoadFromDir(newPath) if err != nil { - t.Fatalf("Failed to load newly created chart %q: %s", newPath, err) + assert.FailNowf(t, "Failed to load newly created chart %q: %s", newPath, err) } } func TestChartBuildChart(t *testing.T) { tdir, err := ioutil.TempDir("./", "output-") if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } defer func() { err = os.RemoveAll(tdir) if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } }() err = exec.Command("cp", "-r", "testdata/sample/helm", tdir+"/").Run() if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } err = exec.Command("cp", "-r", "testdata/sample/deps", tdir+"/").Run() if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } _, err = chart.Build(tdir + "/helm") if err != nil { - t.Fatalf("Failed to BuildChart(): %s", err) + assert.FailNowf(t, "Failed to BuildChart(): %s", err.Error()) } chartsDir := tdir + "/helm/charts/" @@ -142,23 +140,23 @@ func TestChartBuildChart(t *testing.T) { func TestZipChartToDir(t *testing.T) { tdir, err := ioutil.TempDir("./", "output-") if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } defer func() { err = os.RemoveAll(tdir) if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } }() helm, err := chart.LoadFromDir("testdata/sample/helm") if err != nil { - t.Fatalf("Failed on LoadChartFromDir(): %s", err) + assert.FailNowf(t, "Failed on LoadChartFromDir(): %s", err.Error()) } _, err = chart.ZipToDir(helm, tdir) if err != nil { - t.Fatalf("Failed on ZipChartToDir(): %s", err) + assert.FailNowf(t, "Failed on ZipChartToDir(): %s", err.Error()) } compareFiles(t, "testdata/golden/test-0.1.0.tgz", tdir+"/test-0.1.0.tgz") } @@ -181,30 +179,30 @@ func compareFiles(t *testing.T, expected, test string) { func readGz(t *testing.T, file string) (out bytes.Buffer) { f, err := os.Open(file) if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } defer func() { err = f.Close() if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } }() zw, err := gzip.NewReader(f) if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } defer func() { err = zw.Close() if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } }() writer := bufio.NewWriter(&out) _, err = io.Copy(writer, zw) if err != nil { - t.Fatal(err) + assert.FailNow(t, err.Error()) } return } From 293af35443dc85a23ec3d64ce0fdbcf73ef4cc94 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 17:30:32 +1100 Subject: [PATCH 77/85] Updated constellation doc. --- internal/platform/constellation/constellation.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/platform/constellation/constellation.go b/internal/platform/constellation/constellation.go index 51f0c44..7f92e16 100644 --- a/internal/platform/constellation/constellation.go +++ b/internal/platform/constellation/constellation.go @@ -1,14 +1,16 @@ package constellation //////////////////////////////////////////////////////////// -// DagConfig class - information for a deployment regarding -// the Services and the Relationships between them. +// Constellation class - information for a deployment +// regarding the Services and the Relationships between +// them. // -// Usual starting point would be to construct a DatConfig -// instance from the corresponding yaml using either: -// dcPointer := NewDagConfigFromFile() +// Usual starting point would be to construct a +// constellation.Config instance from the corresponding +// yaml using either: +// dcPointer := constellation.LoadFile() // or -// dcPointer := NewDagConfigFromString() +// dcPointer := constellation.LoadString() // // Parsing failures are indicated by a nil return. //////////////////////////////////////////////////////////// From 6eef85979051455fd731431b4863026866ac4f54 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 17:31:47 +1100 Subject: [PATCH 78/85] Updated mapper doc. --- internal/platform/mapper/mapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/platform/mapper/mapper.go b/internal/platform/mapper/mapper.go index c26c3ba..4bb3a83 100644 --- a/internal/platform/mapper/mapper.go +++ b/internal/platform/mapper/mapper.go @@ -1,7 +1,7 @@ package mapper ////////////////////////////////////////////////////// -// BuildMapService: Process map files relating services +// Mapper: Process map files relating services // to helm chart files. For example, see accompanying // test file. ////////////////////////////////////////////////////// From 9d51dd19952d3674e03b49e83b41581af69969be Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 17:34:32 +1100 Subject: [PATCH 79/85] Cleanup constellation_test.go. --- internal/platform/constellation/constellation_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/platform/constellation/constellation_test.go b/internal/platform/constellation/constellation_test.go index bd76a90..c466fa2 100644 --- a/internal/platform/constellation/constellation_test.go +++ b/internal/platform/constellation/constellation_test.go @@ -12,7 +12,7 @@ import ( func TestNewDagConfigFromString(t *testing.T) { contentBytes, err := ioutil.ReadFile("testdata/valid.yaml") if nil != err { - t.Fatal(err) + assert.FailNow(t, err.Error()) } type targs struct { @@ -35,7 +35,7 @@ func TestNewDagConfigFromString(t *testing.T) { dag := &constellation.Config{} err := dag.LoadString(tt.args.yamlString) if (err != nil) != tt.wantErr { - t.Errorf("LoadDagConfigFromString() error = %v, wantErr %v", err, tt.wantErr) + assert.Fail(t, "LoadDagConfigFromString() error = %v, wantErr %v", err, tt.wantErr) return } assert.Truef(t, reflect.DeepEqual(dag, tt.wantRet), "LoadDagConfigFromString() =\n%#v,\nWant:\n%#v\n", dag, tt.wantRet) From 2391bbe02f31905b0a6664526f58217f373d48e5 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 17:38:25 +1100 Subject: [PATCH 80/85] FailNow in graph_test.go. --- internal/platform/constellation/graph_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/platform/constellation/graph_test.go b/internal/platform/constellation/graph_test.go index 16c89a8..bbbd2d4 100644 --- a/internal/platform/constellation/graph_test.go +++ b/internal/platform/constellation/graph_test.go @@ -16,7 +16,7 @@ func TestGenerateGraph(t *testing.T) { assert.NoError(t, err) if err != nil { - panic(err) + assert.FailNow(t, err.Error()) } out := &bytes.Buffer{} From 77e37fc5d13d267370f8091e358fc94cc336564d Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 17:43:00 +1100 Subject: [PATCH 81/85] Newline. --- scripts/rbac.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/rbac.yaml b/scripts/rbac.yaml index 07980da..71810d9 100644 --- a/scripts/rbac.yaml +++ b/scripts/rbac.yaml @@ -15,3 +15,4 @@ apiVersion: v1 kind: ServiceAccount metadata: name: admin-role + From 155363876b6067aa7d0bd0ffcbd17cd0e27cd50d Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 17:45:19 +1100 Subject: [PATCH 82/85] Assert in mapper_test.go. --- internal/platform/mapper/mapper_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/platform/mapper/mapper_test.go b/internal/platform/mapper/mapper_test.go index e0160e8..a1036bc 100644 --- a/internal/platform/mapper/mapper_test.go +++ b/internal/platform/mapper/mapper_test.go @@ -12,7 +12,7 @@ import ( func TestMapFromString(t *testing.T) { contentBytes, err := ioutil.ReadFile("testdata/mapper.yaml") if nil != err { - t.Fatal(err) + assert.FailNow(t, err.Error()) } type args struct { @@ -36,7 +36,7 @@ func TestMapFromString(t *testing.T) { mapper := &mapper.Config{} err := mapper.LoadString(tt.args.yamlString) if (err != nil) != tt.wantErr { - t.Errorf("LoadMapFromString() error = %v, wantErr %v", err, tt.wantErr) + assert.Fail(t, "LoadMapFromString() error = %v, wantErr %v", err, tt.wantErr) return } assert.Truef(t, reflect.DeepEqual(mapper, tt.wantRet), "LoadMapFromString() = %v, want %v", mapper, tt.wantRet) From 0dde3db7a6c5501b1619e75ced0e4d230a3853d2 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 17:46:55 +1100 Subject: [PATCH 83/85] Assert guid_test.go. --- tools/guid/guid_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/guid/guid_test.go b/tools/guid/guid_test.go index 39aedf2..987526d 100644 --- a/tools/guid/guid_test.go +++ b/tools/guid/guid_test.go @@ -2,6 +2,7 @@ package guid_test import ( "github.com/microsoft/abstrakt/tools/guid" + "github.com/stretchr/testify/assert" "testing" ) @@ -60,9 +61,7 @@ func TestGUID_Equals(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.LHS.Equals(tt.args.RHS); got != tt.want { - t.Errorf("GUID.Equals() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.LHS.Equals(tt.args.RHS)) }) } } From 7e537d9796a5bf90abe4ed4c786e17275a51c21e Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 18:34:36 +1100 Subject: [PATCH 84/85] Check if compose was able to load files or not. --- cmd/compose.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/compose.go b/cmd/compose.go index de5d365..e648500 100644 --- a/cmd/compose.go +++ b/cmd/compose.go @@ -44,7 +44,11 @@ Example: abstrakt compose [chart name] -t [templateType] -f [constellationFilePa } service := new(compose.Composer) - _ = service.LoadFile(cc.constellationFilePath, cc.mapsFilePath) + err = service.LoadFile(cc.constellationFilePath, cc.mapsFilePath) + + if err != nil { + return + } logger.Debugf("noChecks is set to %t", *cc.noChecks) From 45f20493c7e0f8d63c14934255bcb597c45b2c59 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 7 Feb 2020 18:35:16 +1100 Subject: [PATCH 85/85] Removed `file.Exists()` as it's not needed. --- cmd/diff.go | 13 ++----------- cmd/diff_test.go | 6 +++--- cmd/visualise.go | 7 +------ cmd/visualise_test.go | 4 ++-- 4 files changed, 8 insertions(+), 22 deletions(-) diff --git a/cmd/diff.go b/cmd/diff.go index 162c541..fa8ef57 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/microsoft/abstrakt/internal/diff" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/tools/file" "github.com/microsoft/abstrakt/tools/logger" "github.com/spf13/cobra" "strings" @@ -39,18 +38,10 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP logger.Debugf("showOriginalOutput: %t", *cc.showOriginal) logger.Debugf("showNewOutput: %t", *cc.showNew) - if !file.Exists(cc.constellationFilePathOrg) { - return fmt.Errorf("Could not open original YAML input file for reading %v", cc.constellationFilePathOrg) - } - - if !file.Exists(cc.constellationFilePathNew) { - return fmt.Errorf("Could not open new YAML input file for reading %v", cc.constellationFilePathNew) - } - dsGraphOrg := new(constellation.Config) err := dsGraphOrg.LoadFile(cc.constellationFilePathOrg) if err != nil { - return fmt.Errorf("dagConfigService failed to load file %q: %s", cc.constellationFilePathOrg, err) + return fmt.Errorf("Constellation config failed to load file %q: %s", cc.constellationFilePathOrg, err) } if *cc.showOriginal { @@ -66,7 +57,7 @@ Example: abstrakt diff -o [constellationFilePathOriginal] -n [constellationFileP dsGraphNew := new(constellation.Config) err = dsGraphNew.LoadFile(cc.constellationFilePathNew) if err != nil { - return fmt.Errorf("dagConfigService failed to load file %q: %s", cc.constellationFilePathNew, err) + return fmt.Errorf("Constellation config failed to load file %q: %s", cc.constellationFilePathNew, err) } if *cc.showNew { diff --git a/cmd/diff_test.go b/cmd/diff_test.go index c40c4d9..dbc4639 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -22,14 +22,14 @@ func TestDiffCmdWithAllRequirementsNoError(t *testing.T) { // TestDffCmdFailYaml - test diff command parameters // Test both required command line parameters (-o, -n) failing each in turn func TestDffCmdFailYaml(t *testing.T) { - expected := "Could not open original YAML input file for reading constellationPathOrg" + expected := "Constellation config failed to load file \"constellationPathOrg\": open constellationPathOrg: no such file or directory" _, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", "constellationPathOrg", "-n", "constellationPathNew") assert.Error(t, err) assert.EqualError(t, err, expected) - expected = "Could not open new YAML input file for reading constellationPathNew" + expected = "Constellation config failed to load file \"constellationPathNew\": open constellationPathNew: no such file or directory" _, err = helper.ExecuteCommand(newDiffCmd().cmd, "-o", "../examples/constellation/sample_constellation.yaml", "-n", "constellationPathNew") @@ -40,7 +40,7 @@ func TestDffCmdFailYaml(t *testing.T) { // TestDiffCmdFailNotYaml - test diff command parameters // Test both required command line parameter files fail when provided with invalid input files (-o, -n) failing each in turn func TestDiffCmdFailNotYaml(t *testing.T) { - expected := "dagConfigService failed to load file \"diff.go\": yaml: line 26: mapping values are not allowed in this context" + expected := "Constellation config failed to load file \"diff.go\": yaml: line 25: mapping values are not allowed in this context" _, err := helper.ExecuteCommand(newDiffCmd().cmd, "-o", "diff.go", "-n", "diff.go") diff --git a/cmd/visualise.go b/cmd/visualise.go index 8361d9c..019048f 100644 --- a/cmd/visualise.go +++ b/cmd/visualise.go @@ -9,7 +9,6 @@ import ( "bytes" "fmt" "github.com/microsoft/abstrakt/internal/platform/constellation" - "github.com/microsoft/abstrakt/tools/file" "github.com/microsoft/abstrakt/tools/logger" "github.com/spf13/cobra" "strings" @@ -34,14 +33,10 @@ Example: abstrakt visualise -f [constellationFilePath]`, logger.Debug("args: " + strings.Join(args, " ")) logger.Debug("constellationFilePath: " + cc.constellationFilePath) - if !file.Exists(cc.constellationFilePath) { - return fmt.Errorf("Could not open YAML input file for reading %v", cc.constellationFilePath) - } - dsGraph := new(constellation.Config) err := dsGraph.LoadFile(cc.constellationFilePath) if err != nil { - return fmt.Errorf("dagConfigService failed to load file %q: %s", cc.constellationFilePath, err) + return fmt.Errorf("Constellation config failed to load file %q: %s", cc.constellationFilePath, err) } out := &bytes.Buffer{} diff --git a/cmd/visualise_test.go b/cmd/visualise_test.go index 56b3f67..532e68e 100644 --- a/cmd/visualise_test.go +++ b/cmd/visualise_test.go @@ -23,7 +23,7 @@ func TestVisualiseCmdWithAllRequirementsNoError(t *testing.T) { } func TestVisualiseCmdFailYaml(t *testing.T) { - expected := "Could not open YAML input file for reading" + expected := "Constellation config failed to load file" _, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", "constellationPath") @@ -32,7 +32,7 @@ func TestVisualiseCmdFailYaml(t *testing.T) { } func TestVisualiseCmdFailNotYaml(t *testing.T) { - expected := "dagConfigService failed to load file" + expected := "Constellation config failed to load file" _, err := helper.ExecuteCommand(newVisualiseCmd().cmd, "-f", "visualise.go")