...

Source file src/go.opentelemetry.io/otel/sdk/internal/internaltest/env_test.go

Documentation: go.opentelemetry.io/otel/sdk/internal/internaltest

     1  // Code created by gotmpl. DO NOT MODIFY.
     2  // source: internal/shared/internaltest/env_test.go.tmpl
     3  
     4  // Copyright The OpenTelemetry Authors
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package internaltest
    19  
    20  import (
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  	"github.com/stretchr/testify/suite"
    27  )
    28  
    29  type EnvStoreTestSuite struct {
    30  	suite.Suite
    31  }
    32  
    33  func (s *EnvStoreTestSuite) Test_add() {
    34  	envStore := newEnvStore()
    35  
    36  	e := Env{
    37  		Name:   "name",
    38  		Value:  "value",
    39  		Exists: true,
    40  	}
    41  	envStore.add(e)
    42  	envStore.add(e)
    43  
    44  	s.Assert().Len(envStore.store, 1)
    45  }
    46  
    47  func (s *EnvStoreTestSuite) TestRecord() {
    48  	testCases := []struct {
    49  		name             string
    50  		env              Env
    51  		expectedEnvStore *envStore
    52  	}{
    53  		{
    54  			name: "record exists env",
    55  			env: Env{
    56  				Name:   "name",
    57  				Value:  "value",
    58  				Exists: true,
    59  			},
    60  			expectedEnvStore: &envStore{store: map[string]Env{
    61  				"name": {
    62  					Name:   "name",
    63  					Value:  "value",
    64  					Exists: true,
    65  				},
    66  			}},
    67  		},
    68  		{
    69  			name: "record exists env, but its value is empty",
    70  			env: Env{
    71  				Name:   "name",
    72  				Value:  "",
    73  				Exists: true,
    74  			},
    75  			expectedEnvStore: &envStore{store: map[string]Env{
    76  				"name": {
    77  					Name:   "name",
    78  					Value:  "",
    79  					Exists: true,
    80  				},
    81  			}},
    82  		},
    83  		{
    84  			name: "record not exists env",
    85  			env: Env{
    86  				Name:   "name",
    87  				Exists: false,
    88  			},
    89  			expectedEnvStore: &envStore{store: map[string]Env{
    90  				"name": {
    91  					Name:   "name",
    92  					Exists: false,
    93  				},
    94  			}},
    95  		},
    96  	}
    97  
    98  	for _, tc := range testCases {
    99  		s.Run(tc.name, func() {
   100  			if tc.env.Exists {
   101  				s.Assert().NoError(os.Setenv(tc.env.Name, tc.env.Value))
   102  			}
   103  
   104  			envStore := newEnvStore()
   105  			envStore.Record(tc.env.Name)
   106  
   107  			s.Assert().Equal(tc.expectedEnvStore, envStore)
   108  
   109  			if tc.env.Exists {
   110  				s.Assert().NoError(os.Unsetenv(tc.env.Name))
   111  			}
   112  		})
   113  	}
   114  }
   115  
   116  func (s *EnvStoreTestSuite) TestRestore() {
   117  	testCases := []struct {
   118  		name              string
   119  		env               Env
   120  		expectedEnvValue  string
   121  		expectedEnvExists bool
   122  	}{
   123  		{
   124  			name: "exists env",
   125  			env: Env{
   126  				Name:   "name",
   127  				Value:  "value",
   128  				Exists: true,
   129  			},
   130  			expectedEnvValue:  "value",
   131  			expectedEnvExists: true,
   132  		},
   133  		{
   134  			name: "no exists env",
   135  			env: Env{
   136  				Name:   "name",
   137  				Exists: false,
   138  			},
   139  			expectedEnvExists: false,
   140  		},
   141  	}
   142  
   143  	for _, tc := range testCases {
   144  		s.Run(tc.name, func() {
   145  			envStore := newEnvStore()
   146  			envStore.add(tc.env)
   147  
   148  			// Backup
   149  			backup := newEnvStore()
   150  			backup.Record(tc.env.Name)
   151  
   152  			s.Require().NoError(os.Unsetenv(tc.env.Name))
   153  
   154  			s.Assert().NoError(envStore.Restore())
   155  			v, exists := os.LookupEnv(tc.env.Name)
   156  			s.Assert().Equal(tc.expectedEnvValue, v)
   157  			s.Assert().Equal(tc.expectedEnvExists, exists)
   158  
   159  			// Restore
   160  			s.Require().NoError(backup.Restore())
   161  		})
   162  	}
   163  }
   164  
   165  func (s *EnvStoreTestSuite) Test_setEnv() {
   166  	testCases := []struct {
   167  		name              string
   168  		key               string
   169  		value             string
   170  		expectedEnvStore  *envStore
   171  		expectedEnvValue  string
   172  		expectedEnvExists bool
   173  	}{
   174  		{
   175  			name:  "normal",
   176  			key:   "name",
   177  			value: "value",
   178  			expectedEnvStore: &envStore{store: map[string]Env{
   179  				"name": {
   180  					Name:   "name",
   181  					Value:  "other value",
   182  					Exists: true,
   183  				},
   184  			}},
   185  			expectedEnvValue:  "value",
   186  			expectedEnvExists: true,
   187  		},
   188  	}
   189  
   190  	for _, tc := range testCases {
   191  		s.Run(tc.name, func() {
   192  			envStore := newEnvStore()
   193  
   194  			// Backup
   195  			backup := newEnvStore()
   196  			backup.Record(tc.key)
   197  
   198  			s.Require().NoError(os.Setenv(tc.key, "other value"))
   199  
   200  			s.Assert().NoError(envStore.setEnv(tc.key, tc.value))
   201  			s.Assert().Equal(tc.expectedEnvStore, envStore)
   202  			v, exists := os.LookupEnv(tc.key)
   203  			s.Assert().Equal(tc.expectedEnvValue, v)
   204  			s.Assert().Equal(tc.expectedEnvExists, exists)
   205  
   206  			// Restore
   207  			s.Require().NoError(backup.Restore())
   208  		})
   209  	}
   210  }
   211  
   212  func TestEnvStoreTestSuite(t *testing.T) {
   213  	suite.Run(t, new(EnvStoreTestSuite))
   214  }
   215  
   216  func TestSetEnvVariables(t *testing.T) {
   217  	envs := map[string]string{
   218  		"name1": "value1",
   219  		"name2": "value2",
   220  	}
   221  
   222  	// Backup
   223  	backup := newEnvStore()
   224  	for k := range envs {
   225  		backup.Record(k)
   226  	}
   227  	defer func() {
   228  		require.NoError(t, backup.Restore())
   229  	}()
   230  
   231  	store, err := SetEnvVariables(envs)
   232  	assert.NoError(t, err)
   233  	require.IsType(t, &envStore{}, store)
   234  	concreteStore := store.(*envStore)
   235  	assert.Len(t, concreteStore.store, 2)
   236  	assert.Equal(t, backup, concreteStore)
   237  }
   238  

View as plain text