github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/orchestrator/orchestrator_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package orchestrator 5 6 import ( 7 "os" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestOrchestrator(t *testing.T) { 14 t.Run("Not running on CI", func(t *testing.T) { 15 defer resetEnv(os.Environ()) 16 os.Clearenv() 17 18 provider, err := NewOrchestratorSpecificConfigProvider() 19 20 assert.EqualError(t, err, "unable to detect a supported orchestrator (Azure DevOps, GitHub Actions, Jenkins)") 21 assert.Equal(t, "Unknown", provider.OrchestratorType()) 22 }) 23 24 t.Run("Test orchestrator.toString()", func(t *testing.T) { 25 defer resetEnv(os.Environ()) 26 os.Clearenv() 27 28 os.Setenv("AZURE_HTTP_USER_AGENT", "FOO BAR BAZ") 29 30 o := DetectOrchestrator() 31 32 assert.Equal(t, "AzureDevOps", o.String()) 33 }) 34 35 t.Run("Test areIndicatingEnvVarsSet", func(t *testing.T) { 36 defer resetEnv(os.Environ()) 37 os.Clearenv() 38 39 envVars := []string{"GITHUB_ACTION", "GITHUB_ACTIONS"} 40 41 os.Setenv("GITHUB_ACTION", "true") 42 tmp := areIndicatingEnvVarsSet(envVars) 43 assert.True(t, tmp) 44 45 os.Unsetenv("GITHUB_ACTION") 46 os.Setenv("GITHUB_ACTIONS", "true") 47 tmp = areIndicatingEnvVarsSet(envVars) 48 assert.True(t, tmp) 49 50 os.Setenv("GITHUB_ACTION", "1") 51 os.Setenv("GITHUB_ACTIONS", "false") 52 tmp = areIndicatingEnvVarsSet(envVars) 53 assert.True(t, tmp) 54 55 os.Setenv("GITHUB_ACTION", "false") 56 os.Setenv("GITHUB_ACTIONS", "0") 57 tmp = areIndicatingEnvVarsSet(envVars) 58 assert.False(t, tmp) 59 }) 60 } 61 62 func Test_getEnv(t *testing.T) { 63 type args struct { 64 key string 65 fallback string 66 } 67 tests := []struct { 68 name string 69 args args 70 want string 71 envVar string 72 }{ 73 { 74 name: "environment variable found", 75 args: args{key: "debug", fallback: "fallback"}, 76 want: "found", 77 envVar: "debug", 78 }, 79 { 80 name: "fallback variable", 81 args: args{key: "debug", fallback: "fallback"}, 82 want: "fallback", 83 }, 84 } 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 defer resetEnv(os.Environ()) 88 os.Clearenv() 89 os.Setenv(tt.envVar, "found") 90 assert.Equalf(t, tt.want, getEnv(tt.args.key, tt.args.fallback), "getEnv(%v, %v)", tt.args.key, tt.args.fallback) 91 }) 92 } 93 }