From 48b6b3f5b30f38d6c3800921eca74552617af2f1 Mon Sep 17 00:00:00 2001 From: Erin Call Date: Mon, 30 Dec 2019 11:57:19 -0800 Subject: [PATCH] Create AddRepo steps when there are repos to add [#26] --- internal/helm/config.go | 2 +- internal/helm/plan.go | 21 ++++++++++- internal/helm/plan_test.go | 75 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/internal/helm/config.go b/internal/helm/config.go index cf351ae..a4d1914 100644 --- a/internal/helm/config.go +++ b/internal/helm/config.go @@ -18,7 +18,7 @@ type Config struct { Command string `envconfig:"HELM_COMMAND"` // Helm command to run DroneEvent string `envconfig:"DRONE_BUILD_EVENT"` // Drone event that invoked this plugin. UpdateDependencies bool `split_words:"true"` // Call `helm dependency update` before the main command - Repos []string `envconfig:"HELM_REPOS"` // Call `helm repo add` before the main command + AddRepos []string `envconfig:"HELM_REPOS"` // Call `helm repo add` before the main command Prefix string `` // Prefix to use when looking up secret env vars Debug bool `` // Generate debug output and pass --debug to all helm commands Values string `` // Argument to pass to --set in applicable helm commands diff --git a/internal/helm/plan.go b/internal/helm/plan.go index ad2e5ae..303aa7c 100644 --- a/internal/helm/plan.go +++ b/internal/helm/plan.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/pelotech/drone-helm3/internal/run" "os" + "strings" ) const ( @@ -96,6 +97,7 @@ func (p *Plan) Execute() error { var upgrade = func(cfg Config) []Step { steps := initKube(cfg) + steps = append(steps, addRepos(cfg)...) if cfg.UpdateDependencies { steps = append(steps, depUpdate(cfg)...) } @@ -127,7 +129,7 @@ var uninstall = func(cfg Config) []Step { } var lint = func(cfg Config) []Step { - steps := make([]Step, 0) + steps := addRepos(cfg) if cfg.UpdateDependencies { steps = append(steps, depUpdate(cfg)...) } @@ -157,6 +159,23 @@ func initKube(cfg Config) []Step { } } +func addRepos(cfg Config) []Step { + steps := make([]Step, 0) + for _, repo := range cfg.AddRepos { + split := strings.SplitN(repo, "=", 2) + if len(split) != 2 { + fmt.Fprintf(cfg.Stderr, "Warning: skipping bad repo spec '%s'.\n", repo) + continue + } + steps = append(steps, &run.AddRepo{ + Name: split[0], + URL: split[1], + }) + } + + return steps +} + func depUpdate(cfg Config) []Step { return []Step{ &run.DepUpdate{ diff --git a/internal/helm/plan_test.go b/internal/helm/plan_test.go index 2cdde5c..f8db1bd 100644 --- a/internal/helm/plan_test.go +++ b/internal/helm/plan_test.go @@ -177,6 +177,17 @@ func (suite *PlanTestSuite) TestUpgradeWithUpdateDependencies() { suite.IsType(&run.DepUpdate{}, steps[1]) } +func (suite *PlanTestSuite) TestUpgradeWithAddRepos() { + cfg := Config{ + AddRepos: []string{ + "machine=https://github.com/harold_finch/themachine", + }, + } + steps := upgrade(cfg) + suite.Require().True(len(steps) > 1, "upgrade should generate at least two steps") + suite.IsType(&run.AddRepo{}, steps[1]) +} + func (suite *PlanTestSuite) TestUninstall() { cfg := Config{ KubeToken: "b2YgbXkgYWZmZWN0aW9u", @@ -268,6 +279,61 @@ func (suite *PlanTestSuite) TestDepUpdate() { suite.Equal(expected, update) } +func (suite *PlanTestSuite) TestAddRepos() { + cfg := Config{ + AddRepos: []string{ + "first=https://add.repos/one", + "second=https://add.repos/two", + }, + } + steps := addRepos(cfg) + suite.Require().Equal(2, len(steps), "addRepos should add one step per repo") + suite.Require().IsType(&run.AddRepo{}, steps[0]) + suite.Require().IsType(&run.AddRepo{}, steps[1]) + first := steps[0].(*run.AddRepo) + second := steps[1].(*run.AddRepo) + + suite.Equal(first.Name, "first") + suite.Equal(first.URL, "https://add.repos/one") + suite.Equal(second.Name, "second") + suite.Equal(second.URL, "https://add.repos/two") +} + +func (suite *PlanTestSuite) TestAddNoRepos() { + cfg := Config{ + AddRepos: []string{}, + } + steps := addRepos(cfg) + suite.Equal(0, len(steps), "adding no repos should take zero steps") +} + +func (suite *PlanTestSuite) TestAddReposWithMalformedRepoSpec() { + stderr := strings.Builder{} + cfg := Config{ + AddRepos: []string{ + "dwim", + }, + Stderr: &stderr, + } + steps := addRepos(cfg) + suite.Equal(len(steps), 0) + suite.Equal("Warning: skipping bad repo spec 'dwim'.\n", stderr.String()) +} + +func (suite *PlanTestSuite) TestAddReposWithEqualsInURL() { + cfg := Config{ + AddRepos: []string{ + "samaritan=https://github.com/arthur_claypool/samaritan?version=2.1", + }, + Stderr: &strings.Builder{}, + } + steps := addRepos(cfg) + suite.Require().Equal(1, len(steps)) + suite.Require().IsType(&run.AddRepo{}, steps[0]) + addRepo := steps[0].(*run.AddRepo) + suite.Equal("https://github.com/arthur_claypool/samaritan?version=2.1", addRepo.URL) +} + func (suite *PlanTestSuite) TestLint() { cfg := Config{ Chart: "./flow", @@ -291,6 +357,15 @@ func (suite *PlanTestSuite) TestLintWithUpdateDependencies() { suite.IsType(&run.DepUpdate{}, steps[0]) } +func (suite *PlanTestSuite) TestLintWithAddRepos() { + cfg := Config{ + AddRepos: []string{"friendczar=https://github.com/logan_pierce/friendczar"}, + } + steps := lint(cfg) + suite.Require().True(len(steps) > 0, "lint should return at least one step") + suite.IsType(&run.AddRepo{}, steps[0]) +} + func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() { cfg := Config{ Command: "upgrade",