blob: e0f0ac407aa0843a0479652d855ff3fbc9f56dd8 [file] [log] [blame]
Scott Bakere702d122019-10-22 11:54:12 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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// This file implements an exit handler that tries to shut down all the
18// running servers before finally exiting. There are 2 triggers to this
19// clean exit thread: signals and an exit channel.
20
21package main
22
23import (
24 "github.com/stretchr/testify/assert"
25 "io/ioutil"
26 "os"
27 "testing"
28)
29
30// run the function fp() and return its return value and stdout
31func CaptureStdout(fp func() int) (int, string, error) {
32 origStdout := os.Stdout
33
34 // log.Cleanup() will call Sync on sys.stdout, and that doesn't
35 // work on pipes. Instead of creating a pipe, write the output
36 // to a file, then read that file back in.
37 f, err := ioutil.TempFile("", "arouter.json")
38 if err != nil {
39 return 0, "", err
40 }
41
42 // Make sure the file is closed and deleted on exit
43 defer func() { f.Close(); os.Remove(f.Name()) }()
44
45 // reassign stdout to the file, ensure it will be restored on exit
46 os.Stdout = f
47 defer func() { os.Stdout = origStdout }()
48
49 status := fp()
50
51 // read back the contents of the tempfile
52 _, err = f.Seek(0, 0)
53 if err != nil {
54 return 0, "", err
55 }
56 out := make([]byte, 16384)
57 numRead, err := f.Read(out)
58 if err != nil {
59 return 0, "", err
60 }
61
62 return status, string(out[:numRead]), nil
63}
64
65// Test output of "--version" command
66func TestStartupVersionOnly(t *testing.T) {
67 oldArgs := os.Args
68 defer func() { os.Args = oldArgs }()
69
70 os.Args = []string{os.Args[0], "--version"}
71
72 status, s, err := CaptureStdout(startup)
73 assert.Nil(t, err)
74
75 assert.Equal(t, 0, status)
76
77 expected := `VOLTHA API Server (afrouterd)
78 Version: unknown-version
79 GoVersion: unknown-goversion
80 VCS Ref: unknown-vcsref
81 VCS Dirty: unknown-vcsdirty
82 Built: unknown-buildtime
83 OS/Arch: unknown-os/unknown-arch
84
85`
86 assert.Equal(t, expected, s)
87}
88
89// An unknown command-line option should produce an error
90func TestStartupBadCommandLine(t *testing.T) {
91 oldArgs := os.Args
92 defer func() { os.Args = oldArgs }()
93
94 os.Args = []string{os.Args[0], "--badoption"}
95
96 status, s, err := CaptureStdout(startup)
97 assert.Nil(t, err)
98
99 assert.Equal(t, 1, status)
100
101 assert.Contains(t, s, "Error: flag provided but not defined: -badoption")
102}