Debian Codegraph @

pkg/config/config_test.go

raw
1 // Copyright 2023 The Archivista Contributors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package config
16
17 import (
18 "bytes"
19 "os"
20 "testing"
21
22 "github.com/sirupsen/logrus"
23 "github.com/stretchr/testify/require"
24 )
25
26 func TestConfig_Process(t *testing.T) {
27 // Set up test environment variables
28 os.Setenv("ARCHIVISTA_LISTEN_ON", "tcp://0.0.0.0:8082")
29 os.Setenv("ARCHIVISTA_READ_TIMEOUT", "300")
30 os.Setenv("ARCHIVISTA_WRITE_TIMEOUT", "300")
31 os.Setenv("ARCHIVISTA_LOG_LEVEL", "DEBUG")
32 os.Setenv("ARCHIVISTA_CORS_ALLOW_ORIGINS", "http://localhost,https://example.com")
33 os.Setenv("ARCHIVISTA_ENABLE_SPIFFE", "FALSE")
34 os.Setenv("ARCHIVISTA_SQL_STORE_CONNECTION_STRING", "root:password@tcp(localhost:3306)/testify")
35 os.Setenv("ARCHIVISTA_STORAGE_BACKEND", "BLOB")
36 os.Setenv("ARCHIVISTA_BLOB_STORE_ENDPOINT", "https://s3.example.com")
37 os.Setenv("ARCHIVISTA_BLOB_STORE_ACCESS_KEY_ID", "ACCESSKEYID")
38 os.Setenv("ARCHIVISTA_BLOB_STORE_SECRET_ACCESS_KEY_ID", "SECRETACCESSKEYID")
39 os.Setenv("ARCHIVISTA_BLOB_STORE_BUCKET_NAME", "mybucket")
40 os.Setenv("ARCHIVISTA_GRAPHQL_WEB_CLIENT_ENABLE", "FALSE")
41
42 // Create a Config struct and call Process()
43 c := &Config{}
44 err := c.Process()
45 require.NoError(t, err)
46
47 // Check that the expected values were read from environment variables
48 require.Equal(t, "tcp://0.0.0.0:8082", c.ListenOn)
49 require.Equal(t, 300, c.ReadTimeout)
50 require.Equal(t, 300, c.WriteTimeout)
51 require.Equal(t, "DEBUG", c.LogLevel)
52 require.Equal(t, []string{"http://localhost", "https://example.com"}, c.CORSAllowOrigins)
53 require.False(t, c.EnableSPIFFE)
54 require.Equal(t, "root:password@tcp(localhost:3306)/testify", c.SQLStoreConnectionString)
55 require.Equal(t, "BLOB", c.StorageBackend)
56 require.Equal(t, "https://s3.example.com", c.BlobStoreEndpoint)
57 require.Equal(t, "ACCESSKEYID", c.BlobStoreAccessKeyId)
58 require.Equal(t, "SECRETACCESSKEYID", c.BlobStoreSecretAccessKeyId)
59 require.Equal(t, "mybucket", c.BlobStoreBucketName)
60 require.False(t, c.GraphqlWebClientEnable)
61
62 // Clean up environment variables
63 os.Unsetenv("ARCHIVISTA_LISTEN_ON")
64 os.Unsetenv("ARCHIVISTA_READ_TIMEOUT")
65 os.Unsetenv("ARCHIVISTA_WRITE_TIMEOUT")
66 os.Unsetenv("ARCHIVISTA_LOG_LEVEL")
67 os.Unsetenv("ARCHIVISTA_CORS_ALLOW_ORIGINS")
68 os.Unsetenv("ARCHIVISTA_ENABLE_SPIFFE")
69 os.Unsetenv("ARCHIVISTA_SQL_STORE_CONNECTION_STRING")
70 os.Unsetenv("ARCHIVISTA_STORAGE_BACKEND")
71 os.Unsetenv("ARCHIVISTA_FILE_SERVE_ON")
72 os.Unsetenv("ARCHIVISTA_FILE_DIR")
73 os.Unsetenv("ARCHIVISTA_BLOB_STORE_ENDPOINT")
74 os.Unsetenv("ARCHIVISTA_BLOB_STORE_ACCESS_KEY_ID")
75 os.Unsetenv("ARCHIVISTA_BLOB_STORE_SECRET_ACCESS_KEY_ID")
76 os.Unsetenv("ARCHIVISTA_BLOB_STORE_BUCKET_NAME")
77 os.Unsetenv("ARCHIVISTA_ENABLE_GRAPHQL")
78 os.Unsetenv("ARCHIVISTA_GRAPHQL_WEB_CLIENT_ENABLE")
79 }
80
81 func TestConfig_DeprecationAndPrecedence(t *testing.T) {
82 // Set up test environment variables
83 os.Setenv("ARCHIVIST_LISTEN_ON", "tcp://0.0.0.0:8082")
84 os.Setenv("ARCHIVIST_LOG_LEVEL", "DEBUG")
85 os.Setenv("ARCHIVIST_CORS_ALLOW_ORIGINS", "http://localhost:8080")
86
87 // os.Setenv("ARCHIVISTA_LOG_LEVEL", "INFO")
88 // os.Setenv("ARCHIVISTA_CORS_ALLOW_ORIGINS", "http://localhost,https://example.com")
89
90 // Set up log output capturing
91 var output bytes.Buffer
92 logrus.SetOutput(&output)
93 defer func() { logrus.SetOutput(os.Stderr) }()
94
95 // Create a Config struct and call Process()
96 c := &Config{}
97 err := c.Process()
98 require.NoError(t, err)
99
100 //check that the non deprecated environment variables work
101
102 // Check that the deprecated variables work
103 require.Equal(t, "tcp://0.0.0.0:8082", c.ListenOn)
104 require.Equal(t, "DEBUG", c.LogLevel)
105 require.Equal(t, []string{"http://localhost:8080"}, c.CORSAllowOrigins)
106
107 // Check that the appropriate error is returned if both old and new prefixes are used
108 os.Setenv("ARCHIVIST_LISTEN_ON", "tcp://0.0.0.0:8083")
109 os.Setenv("ARCHIVISTA_LISTEN_ON", "tcp://0.0.0.0:8084")
110 c = &Config{}
111 err = c.Process()
112 require.Error(t, err)
113 require.Equal(t, "both deprecated and new environment variables are being used. Please use only the new environment variables", err.Error())
114
115 // Check that the deprecated environment variables were logged
116 require.Contains(t, output.String(), "Using deprecated environment variable ARCHIVIST_LOG_LEVEL")
117 require.Contains(t, output.String(), "Using deprecated environment variable ARCHIVIST_CORS_ALLOW_ORIGINS")
118
119 // Clean up environment variables
120 os.Unsetenv("ARCHIVIST_LISTEN_ON")
121 os.Unsetenv("ARCHIVIST_LOG_LEVEL")
122 os.Unsetenv("ARCHIVISTA_LOG_LEVEL")
123 os.Unsetenv("ARCHIVIST_CORS_ALLOW_ORIGINS")
124 os.Unsetenv("ARCHIVISTA_CORS_ALLOW_ORIGINS")
125 os.Unsetenv("ARCHIVISTA_LISTEN_ON")
126 }