github.com/RedHatInsights/insights-content-service@v1.0.0/content-service_test.go (about) 1 /* 2 Copyright © 2020, 2021, 2022 Red Hat, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main_test 18 19 import ( 20 "bytes" 21 "strings" 22 "testing" 23 24 "github.com/rs/zerolog" 25 "github.com/rs/zerolog/log" 26 "github.com/stretchr/testify/assert" 27 28 "github.com/tisnik/go-capture" 29 30 main "github.com/RedHatInsights/insights-content-service" 31 "github.com/RedHatInsights/insights-content-service/conf" 32 ) 33 34 // checkStandardOutputStatus tests whether the standard output capturing was successful 35 func checkStandardOutputStatus(t *testing.T, err error) { 36 if err != nil { 37 t.Fatal("Unable to capture standard output", err) 38 } 39 } 40 41 // checkStandardOutputNotEmpty tests if standard output capturing captured at least some text 42 func checkStandardOutputNotEmpty(t *testing.T, captured string) { 43 if captured == "" { 44 t.Fatal("Output is empty") 45 } 46 } 47 48 // checkHelpContent tests the help message displayed on standard output 49 func checkHelpContent(t *testing.T, captured string) { 50 checkStandardOutputNotEmpty(t, captured) 51 if !strings.HasPrefix(captured, "\nService to provide content for OCP rules") { 52 t.Fatal("Unexpected help text") 53 } 54 } 55 56 // checkVersionContent tests the help version info displayed on standard output 57 func checkVersionContent(t *testing.T, captured string) { 58 checkStandardOutputNotEmpty(t, captured) 59 if !strings.HasPrefix(captured, "Version:\t") { 60 t.Fatal("Unexpected version info") 61 } 62 } 63 64 // checkConfigContent tests the configuration info displayed on standard output 65 func checkConfigContent(t *testing.T, captured string) { 66 checkStandardOutputNotEmpty(t, captured) 67 } 68 69 // checkUnknownCommand tests the unknown command message displayed on standard output 70 func checkUnknownCommand(t *testing.T, captured string) { 71 checkStandardOutputNotEmpty(t, captured) 72 if !strings.HasPrefix(captured, "\nCommand ") { 73 t.Fatal("Unexpected error message") 74 } 75 } 76 77 // TestPrintHelp is dummy ATM - we'll check the actual print content etc. in integration tests 78 func TestPrintHelp(t *testing.T) { 79 captured, err := capture.StandardOutput(func() { 80 main.PrintHelp() 81 }) 82 checkStandardOutputStatus(t, err) 83 checkHelpContent(t, captured) 84 } 85 86 // TestPrintVersionInfo is dummy ATM - we'll check versions etc. in integration tests 87 func TestPrintVersionInfo(t *testing.T) { 88 captured, err := capture.StandardOutput(func() { 89 main.PrintVersionInfo() 90 }) 91 checkStandardOutputStatus(t, err) 92 checkVersionContent(t, captured) 93 } 94 95 // TestPrintConfig is dummy ATM - we'll check config output etc. in integration tests 96 func TestPrintConfig(t *testing.T) { 97 captured, err := capture.StandardOutput(func() { 98 main.PrintConfig(conf.Config) 99 }) 100 checkStandardOutputStatus(t, err) 101 checkConfigContent(t, captured) 102 } 103 104 // TestHandleCommandHelp tests if proper output is printed for commands "help" and "print-help" 105 func TestHandleCommandHelp(t *testing.T) { 106 helpCommands := []string{"help", "print-help"} 107 for _, command := range helpCommands { 108 captured, err := capture.StandardOutput(func() { 109 main.HandleCommand(command) 110 }) 111 checkStandardOutputStatus(t, err) 112 checkHelpContent(t, captured) 113 } 114 } 115 116 // TestHandleCommandConfig tests if proper output is printed for command "print-config" 117 func TestHandleCommandConfig(t *testing.T) { 118 captured, err := capture.StandardOutput(func() { 119 main.HandleCommand("print-config") 120 }) 121 checkStandardOutputStatus(t, err) 122 checkConfigContent(t, captured) 123 } 124 125 // TestHandleCommandVersion tests if proper output is printed for command "print-version-info" 126 func TestHandleCommandVersion(t *testing.T) { 127 captured, err := capture.StandardOutput(func() { 128 main.HandleCommand("print-version-info") 129 }) 130 checkStandardOutputStatus(t, err) 131 checkVersionContent(t, captured) 132 } 133 134 // TestHandleCommmandPrintGroups tests if proper output is printed for command "print-groups" 135 func TestHandleCommandPrintGroups(t *testing.T) { 136 _, err := capture.StandardOutput(func() { 137 main.HandleCommand("print-groups") 138 }) 139 checkStandardOutputStatus(t, err) 140 } 141 142 // TestHandleCommandUnknownInput tests if proper output is printed for unknown command 143 func TestHandleCommandUnknownInput(t *testing.T) { 144 captured, err := capture.StandardOutput(func() { 145 main.HandleCommand("foo-bar-baz") 146 }) 147 checkStandardOutputStatus(t, err) 148 checkUnknownCommand(t, captured) 149 } 150 151 // TestInitInfoLog check the function initInfoLog 152 func TestInitInfoLog(t *testing.T) { 153 buf := new(bytes.Buffer) 154 log.Logger = zerolog.New(buf) 155 156 expectedString := "*** message ***" 157 main.InitInfoLog(expectedString) 158 159 logContent := buf.String() 160 if !strings.Contains(logContent, expectedString) { 161 t.Fatal("Inconsistent log content", logContent) 162 } 163 } 164 165 // TestLogVersionInfo check the function logVersionInfo 166 func TestLogVersionInfo(t *testing.T) { 167 buf := new(bytes.Buffer) 168 log.Logger = zerolog.New(buf) 169 170 main.LogVersionInfo() 171 172 logContent := buf.String() 173 if !strings.Contains(logContent, "Build time:") { 174 t.Fatal("Inconsistent log content", logContent) 175 } 176 } 177 178 // TestPrintGroups check the behaviour of the printGroups function when no groups are configured 179 func TestPrintGroups(t *testing.T) { 180 retval := int(main.PrintGroups()) 181 assert.Equal(t, main.ExitStatusServerError, retval) 182 } 183 184 // TestPrintRules check the behaviour of the printRules function when no rules are configured 185 func TestPrintRules(t *testing.T) { 186 retval := int(main.PrintRules()) 187 assert.Equal(t, main.ExitStatusReadContentError, retval) 188 } 189 190 // TestFillInInfoParams test the behaviour of function fillInInfoParams 191 func TestFillInInfoParams(t *testing.T) { 192 // map to be used by this unit test 193 m := make(map[string]string) 194 195 // preliminary test if Go Universe is still ok 196 assert.Empty(t, m, "Map should be empty at the beginning") 197 198 // try to fill-in all info params 199 main.FillInInfoParams(m) 200 201 // preliminary test if Go Universe is still ok 202 assert.Len(t, m, 6, "Map should contains exactly six items") 203 204 // does the map contain all expected keys? 205 assert.Contains(t, m, "BuildVersion") 206 assert.Contains(t, m, "BuildTime") 207 assert.Contains(t, m, "BuildBranch") 208 assert.Contains(t, m, "BuildCommit") 209 assert.Contains(t, m, "UtilsVersion") 210 assert.Contains(t, m, "OCPRulesVersion") 211 }