github.com/jaylevin/jenkins-library@v1.230.4/pkg/orchestrator/orchestrator_test.go (about)

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