| 1 |
// Copyright 2020 Northern.tech AS |
| 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 |
package main |
| 15 |
|
| 16 |
import ( |
| 17 |
"io/ioutil" |
| 18 |
"os" |
| 19 |
"os/exec" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/stretchr/testify/assert" |
| 23 |
"github.com/stretchr/testify/require" |
| 24 |
) |
| 25 |
|
| 26 |
func TestMainExitCodes(t *testing.T) { |
| 27 |
// Cache args. |
| 28 |
args := os.Args |
| 29 |
// Successfull main call (0) |
| 30 |
os.Args = []string{"mender", "--version"} |
| 31 |
exitCode := doMain() |
| 32 |
assert.Equal(t, 0, exitCode) |
| 33 |
// Ambiguous arguments (1) |
| 34 |
os.Args = []string{"mender", "commit", "install"} |
| 35 |
exitCode = doMain() |
| 36 |
assert.Equal(t, 1, exitCode) |
| 37 |
// Nothing to commit (2) |
| 38 |
storeDir, err := ioutil.TempDir("", "temp_store") |
| 39 |
assert.NoError(t, err) |
| 40 |
os.Args = []string{"mender", "-d", storeDir, "commit"} |
| 41 |
exitCode = doMain() |
| 42 |
assert.Equal(t, 2, exitCode) |
| 43 |
os.Args = args |
| 44 |
} |
| 45 |
|
| 46 |
func TestBinarySize(t *testing.T) { |
| 47 |
// Test that the binary does not unexpectedly increase a lot in size, |
| 48 |
// this is intended to protect against introducing very large |
| 49 |
// dependencies. It is perfectly okay to increase this number as the |
| 50 |
// program grows, but the binary size should be verified before doing |
| 51 |
// so. |
| 52 |
// |
| 53 |
// When increasing, use current binary size on amd64 + 1M. |
| 54 |
const maxSize int64 = 9103464 |
| 55 |
|
| 56 |
cmd := exec.Command("go", "version") |
| 57 |
version, err := cmd.CombinedOutput() |
| 58 |
require.NoError(t, err) |
| 59 |
t.Logf("Go version: %s", string(version)) |
| 60 |
|
| 61 |
programName := "/tmp/mender" |
| 62 |
cmd = exec.Command("go", "build", "-o", programName) |
| 63 |
err = cmd.Run() |
| 64 |
if err != nil { |
| 65 |
t.Fatalf("Could not build '%s': %s", |
| 66 |
programName, err.Error()) |
| 67 |
} |
| 68 |
defer os.Remove(programName) |
| 69 |
|
| 70 |
cmd = exec.Command("strip", programName) |
| 71 |
err = cmd.Run() |
| 72 |
require.NoError(t, err) |
| 73 |
|
| 74 |
statbuf, err := os.Stat(programName) |
| 75 |
|
| 76 |
if err != nil { |
| 77 |
t.Fatalf("Could not stat '%s': %s. Please build before "+ |
| 78 |
"testing.", programName, err.Error()) |
| 79 |
} |
| 80 |
|
| 81 |
if statbuf.Size() > maxSize { |
| 82 |
t.Fatalf("'%s' has grown unexpectedly big (%d bytes). "+ |
| 83 |
"Check that file size is ok?", programName, |
| 84 |
statbuf.Size()) |
| 85 |
} |
| 86 |
} |