diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ef14f8..f406f1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 0.2.0 + +### Features and enhancements + +- [config] read external config file for `manage_tokens` by keyword `include` +- [config] add validation in detecting duplicated manage token +- [config] give the relevan sequence of errors detected in `manage_tokens` config, for easier in config trouble shooting +- [log] introduce arg `--log-json` for logging in JSON format +- [hook] passing additional env var in `exec_cmd` by args `env` + # 0.1.1 ### Bug Fixes diff --git a/README.md b/README.md index 10f6549..a55a0ee 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Consist of YAML formatted content, see the sample one in [sample-config.yml](./e - Accepted duration suffixes: `d` (day), `M` (month), `Y` (year). - Required arguments for hook types: - `update_var`: `.type` (repository or group), `.path` (location), and `name` (variable name). - - `exec_cmd`: `.path` (location of executable) with `GL_NEW_TOKEN` as injected environment variable for the new token. + - `exec_cmd`: `.path` (location of executable) with `GL_NEW_TOKEN` as injected environment variable for the new token, you can pass another env variables using `.env`. - `use_token`: not requiring any arguments, it will uses the new token in the current API call; can only be set once as the first hook. ## Development diff --git a/app/app.go b/app/app.go index cc2800e..1948fec 100644 --- a/app/app.go +++ b/app/app.go @@ -137,8 +137,9 @@ func (g GitlabTokenUpdater) execHook(hk cfg.Hook, newToken string) (err error) { if g.dryRun { return g.sh.FileMustExists(args.Path) } + args.EnvVar[injectEnvVarShellExec] = newToken - results, err := g.sh.Exec(args.Path, map[string]string{injectEnvVarShellExec: newToken}) + results, err := g.sh.Exec(args.Path, args.EnvVar) log.Debug().Msgf("script execution results %s", string(results)) return err } diff --git a/app/app_test.go b/app/app_test.go index b75ac3c..af4d860 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -55,7 +55,15 @@ func TestGitlabTokenUpdater_Do(t *testing.T) { anotherManageTokens[0].Tokens[0].Hooks[0] = t_helper.SampleHookUpdateVarGroup c := t_helper.GenConfig(anotherManageTokens, nil, nil) - c.Managed[0].Tokens[0].Hooks = append(c.Managed[0].Tokens[0].Hooks, t_helper.SampleHookExecScript) + c.Managed[0].Tokens[0].Hooks = append(c.Managed[0].Tokens[0].Hooks, cfg.Hook{ + Type: cfg.HookTypeExecCMD, + Args: map[string]any{ + "path": t_helper.SamplePathToScript, + "env": map[any]any{ + "ENV1": "additional_env", + }, + }, + }) return c }, currentTime: t_helper.GenTime("2024-04-05"), @@ -76,7 +84,8 @@ func TestGitlabTokenUpdater_Do(t *testing.T) { }, mockShell: func(ctrl *gomock.Controller) *sm.MockShell { s := sm.NewMockShell(ctrl) - s.EXPECT().Exec(t_helper.SamplePathToScript, map[string]string{"GL_NEW_TOKEN": "glpat-newnew"}).Return([]byte("abc"), nil) + expectedEnvVar := map[string]string{"GL_NEW_TOKEN": "glpat-newnew", "ENV1": "additional_env"} + s.EXPECT().Exec(t_helper.SamplePathToScript, expectedEnvVar).Return([]byte("abc"), nil) return s }, }, @@ -310,7 +319,7 @@ func TestGitlabTokenUpdater_Do(t *testing.T) { c.Managed[0].Tokens[0].Hooks[0] = cfg.Hook{ Type: cfg.HookTypeUpdateVar, Retry: 1, - Args: map[string]string{ + Args: map[string]any{ "name": t_helper.SampleCICDVar, "path": t_helper.SampleRepoPath, "type": cfg.ManagedTypeRepository, diff --git a/examples/main_config.yml b/examples/main_config.yml index 04641f9..3b6c294 100644 --- a/examples/main_config.yml +++ b/examples/main_config.yml @@ -17,6 +17,9 @@ manage_tokens: retry: 2 args: path: ./scripts/update_secret_manager.sh + env: + GCP_PROJECT: integration-proj + SECRET_ID: integration1 - name: monitoring_purpose expiry_after_rotate: 3M diff --git a/examples/scripts/update_secret_manager.sh b/examples/scripts/update_secret_manager.sh index 9f18185..d105b02 100644 --- a/examples/scripts/update_secret_manager.sh +++ b/examples/scripts/update_secret_manager.sh @@ -1,3 +1,3 @@ #!/bin/bash -echo -n ${GL_NEW_TOKEN} | gcloud secrets --project=abc-proj version add SECRET_ID --dafa-file=- \ No newline at end of file +echo -n ${GL_NEW_TOKEN} | gcloud secrets --project=${GCP_PROJECT} version add "${SECRET_ID}" --dafa-file=- \ No newline at end of file diff --git a/go.mod b/go.mod index 9662983..e120f07 100644 --- a/go.mod +++ b/go.mod @@ -5,16 +5,15 @@ go 1.23.1 require ( github.com/rs/zerolog v1.33.0 github.com/stretchr/testify v1.9.0 - github.com/urfave/cli/v2 v2.27.4 - github.com/xanzy/go-gitlab v0.109.0 + github.com/urfave/cli/v2 v2.27.5 + github.com/xanzy/go-gitlab v0.113.0 go.uber.org/mock v0.4.0 gopkg.in/yaml.v2 v2.4.0 ) require ( - github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-retryablehttp v0.7.7 // indirect @@ -24,11 +23,8 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect - golang.org/x/net v0.8.0 // indirect - golang.org/x/oauth2 v0.6.0 // indirect - golang.org/x/sys v0.20.0 // indirect - golang.org/x/time v0.3.0 // indirect - google.golang.org/appengine v1.6.7 // indirect - google.golang.org/protobuf v1.29.1 // indirect + golang.org/x/oauth2 v0.23.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/time v0.7.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index fa44ba9..5037897 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,14 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= -github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc= +github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= @@ -39,38 +34,24 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8= -github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ= -github.com/xanzy/go-gitlab v0.109.0 h1:RcRme5w8VpLXTSTTMZdVoQWY37qTJWg+gwdQl4aAttE= -github.com/xanzy/go-gitlab v0.109.0/go.mod h1:wKNKh3GkYDMOsGmnfuX+ITCmDuSDWFO0G+C4AygL9RY= +github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w= +github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ= +github.com/xanzy/go-gitlab v0.113.0 h1:v5O4R+YZbJGxKqa9iIZxjMyeKkMKBN8P6sZsNl+YckM= +github.com/xanzy/go-gitlab v0.113.0/go.mod h1:wKNKh3GkYDMOsGmnfuX+ITCmDuSDWFO0G+C4AygL9RY= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= -golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw= -golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= +golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= -golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ= +golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= -google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62UoM= -google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/pkg/config/config.go b/pkg/config/config.go index 8f8a588..2f34da0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -66,13 +66,14 @@ type HookUpdateVar struct { } type HookExecScript struct { - Path string + Path string + EnvVar map[string]string } type Hook struct { - Type string `yaml:"type"` - Retry uint8 `yaml:"retry"` - Args map[string]string `yaml:"args"` + Type string `yaml:"type"` + Retry uint8 `yaml:"retry"` + Args map[string]any `yaml:"args"` } func (h Hook) validate() error { @@ -81,19 +82,19 @@ func (h Hook) validate() error { } if h.Type == HookTypeUpdateVar { - if h.Args["name"] == "" { + if h.Args["name"] == nil { return ErrValidationHookUpdateVarMissingName } - if h.Args["path"] == "" { + if h.Args["path"] == nil { return ErrValidationHookUpdateVarMissingPath } - if !contains(ManagedTypeList, h.Args["type"]) { + if h.Args["type"] != nil && !contains(ManagedTypeList, h.Args["type"].(string)) { return ErrValidationHookUpdateVarInvalidType } } else if h.Type == HookTypeExecCMD { - if h.Args["path"] == "" { + if h.Args["path"] == nil { return ErrValidationHookExecCMDMissingPath } } @@ -102,16 +103,32 @@ func (h Hook) validate() error { func (h Hook) UpdateVarArgs() HookUpdateVar { return HookUpdateVar{ - Name: h.Args["name"], - Path: h.Args["path"], - Type: h.Args["type"], + Name: h.Args["name"].(string), + Path: h.Args["path"].(string), + Type: h.Args["type"].(string), } } +// ExecCMDArgs return the list of argument in execution hook exec_cmd func (h Hook) ExecCMDArgs() HookExecScript { - return HookExecScript{ - Path: h.Args["path"], + execArgs := HookExecScript{ + Path: h.Args["path"].(string), } + execArgs.EnvVar = make(map[string]string) + + if envVar := h.Args["env"]; envVar != nil { + convEnvVar := envVar.(map[any]any) + for key, value := range convEnvVar { + strKey, keyOk := key.(string) + strValue, valueOk := value.(string) + + if keyOk && valueOk { + execArgs.EnvVar[strKey] = strValue + } + } + } + + return execArgs } func (h Hook) StrArgs() string { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 39e8d5b..d922d5e 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -14,13 +14,13 @@ import ( var ( sampleHookExecScript = c.Hook{ Type: c.HookTypeExecCMD, - Args: map[string]string{ + Args: map[string]any{ "path": "./path/to/script.sh", }, } sampleHookUpdateVar = c.Hook{ Type: c.HookTypeUpdateVar, - Args: map[string]string{ + Args: map[string]any{ "name": "SOME_VAR", "path": "/path/to/repo", "type": c.ManagedTypeRepository, @@ -287,7 +287,7 @@ func TestConfig_InitValues_Validations(t *testing.T) { Hooks: []c.Hook{ { Type: c.HookTypeUpdateVar, - Args: map[string]string{ + Args: map[string]any{ "name": "SOME_VAR", "path": "/path/to/repo", "type": "wrong_type", @@ -317,7 +317,7 @@ func TestConfig_InitValues_Validations(t *testing.T) { Hooks: []c.Hook{ { Type: c.HookTypeUpdateVar, - Args: map[string]string{ + Args: map[string]any{ "path": "/some/path", }, }, @@ -344,7 +344,7 @@ func TestConfig_InitValues_Validations(t *testing.T) { Hooks: []c.Hook{ { Type: c.HookTypeUpdateVar, - Args: map[string]string{ + Args: map[string]any{ "name": "CI_VAR", }, }, @@ -449,7 +449,7 @@ func TestConfig_InitValues_Validations(t *testing.T) { Hooks: []c.Hook{ { Type: c.HookTypeExecCMD, - Args: map[string]string{ + Args: map[string]any{ "path": "./some/path", }, }, diff --git a/pkg/config/yaml_test.go b/pkg/config/yaml_test.go index f251105..783ebf6 100644 --- a/pkg/config/yaml_test.go +++ b/pkg/config/yaml_test.go @@ -42,6 +42,8 @@ func TestReadYAMLConfigFile(t *testing.T) { assert.Equal(t, "glpat-abc", cfg.Token) assert.Equal(t, 4, len(cfg.Managed)) assert.Equal(t, uint8(2), cfg.Managed[0].Tokens[0].Hooks[1].Retry) + hookExecCmdCheck := cfg.Managed[0].Tokens[0].Hooks[1].ExecCMDArgs() + assert.Equal(t, "integration-proj", hookExecCmdCheck.EnvVar["GCP_PROJECT"]) }, }, "err: validation check": { diff --git a/pkg/shell/shell_test.go b/pkg/shell/shell_test.go new file mode 100644 index 0000000..563b2ac --- /dev/null +++ b/pkg/shell/shell_test.go @@ -0,0 +1,34 @@ +package shell_test + +import ( + "os" + "testing" + + s "github.com/iomarmochtar/gitlab-token-updater/pkg/shell" + t_helper "github.com/iomarmochtar/gitlab-token-updater/test" + "github.com/stretchr/testify/assert" +) + +func TestSHExecutor_Exec(t *testing.T) { + sh := s.SHExecutor{} + envs := map[string]string{ + "GL_NEW_TOKEN": "glpat-abc", + "ANOTHER_ENV": "abc", + } + result, err := sh.Exec(t_helper.FixturePath("sample_script.sh"), envs) + assert.NoError(t, err) + assert.Equal(t, "new token: glpat-abc, another env: abc", string(result)) + + result, err = sh.Exec("/file/is/not/found", envs) + assert.Nil(t, result) + assert.True(t, os.IsNotExist(err)) +} + +func TestSHExecutor_FileMustExists(t *testing.T) { + sh := s.SHExecutor{} + err := sh.FileMustExists(t_helper.FixturePath("sample_script.sh")) + assert.NoError(t, err) + + err = sh.FileMustExists("/file/is/not/found") + assert.True(t, os.IsNotExist(err)) +} diff --git a/test/fixtures/sample_script.sh b/test/fixtures/sample_script.sh new file mode 100755 index 0000000..d7f1440 --- /dev/null +++ b/test/fixtures/sample_script.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo -n "new token: ${GL_NEW_TOKEN}, another env: ${ANOTHER_ENV}" \ No newline at end of file diff --git a/test/helpers.go b/test/helpers.go index 704fc25..7f02b4a 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -19,13 +19,13 @@ var ( SamplePathToScript = "./path/to/script.sh" SampleHookExecScript = cfg.Hook{ Type: cfg.HookTypeExecCMD, - Args: map[string]string{ + Args: map[string]any{ "path": SamplePathToScript, }, } SampleHookUpdateVarRepo = cfg.Hook{ Type: cfg.HookTypeUpdateVar, - Args: map[string]string{ + Args: map[string]any{ "name": SampleCICDVar, "path": SampleRepoPath, "type": cfg.ManagedTypeRepository, @@ -33,7 +33,7 @@ var ( } SampleHookUpdateVarGroup = cfg.Hook{ Type: cfg.HookTypeUpdateVar, - Args: map[string]string{ + Args: map[string]any{ "name": SampleCICDVar, "path": SampleGroupPath, "type": cfg.ManagedTypeGroup,