| // Copyright 2021 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| // Unit tests for sysapi package |
| package sysapi |
| |
| import ( |
| "io/ioutil" |
| "os" |
| "reflect" |
| "testing" |
| ) |
| |
| // TestAtmoicWriteFile tests that a file is correctly written. It does not test |
| // the atomicity of the write. |
| func TestAtomicWriteFile(t *testing.T) { |
| // Create a temporary directory to write the test file. |
| dir, err := ioutil.TempDir("", "policy_manager_test_") |
| if err != nil { |
| t.Fatal(err) |
| } |
| |
| // Make the test file, to be replaced atomically later. |
| testFileName := dir + "/test" |
| err = ioutil.WriteFile(testFileName, []byte("abc"), 0644) |
| if err != nil { |
| t.Error(err) |
| } |
| |
| // Replace the test file atomically. |
| data := []byte("hello") |
| err = AtomicWriteFile(testFileName, data, 0644) |
| if err != nil { |
| t.Error(err) |
| } |
| |
| // Make sure the file content is updated. |
| content, err := ioutil.ReadFile(testFileName) |
| if err != nil { |
| t.Error(err) |
| } else if !reflect.DeepEqual(content, data) { |
| t.Errorf("got %s, want %s", string(content), string(data)) |
| } |
| |
| // Clean up. |
| if err := os.RemoveAll(dir); err != nil { |
| t.Error(err) |
| } |
| } |
| |
| // TestRunCommandOutput tests whether we can get the correct stdout and stderr |
| // from executing different commands. |
| func TestRunCommandOutput(t *testing.T) { |
| tests := []struct { |
| name string |
| expectStdout string |
| expectStderr string |
| prog string |
| args []string |
| expectErr bool |
| }{ |
| { |
| "EchoStdout", |
| "Hello World!", |
| "", |
| "sh", |
| []string{ |
| "-c", |
| `echo -n "Hello World!"`, |
| }, |
| false, |
| }, |
| { |
| "EchoStderr", |
| "", |
| "Hello World!", |
| "sh", |
| []string{ |
| "-c", |
| `echo -n "Hello World!" 1>&2`, |
| }, |
| false, |
| }, |
| { |
| "NonExistentCommand", |
| "", |
| "", |
| "doesnotexistdon'trunme", |
| nil, |
| true, |
| }, |
| { |
| "OnlyStdout", |
| "Hello World!\n", |
| "", |
| "echo", |
| []string{ |
| `Hello World!`, |
| }, |
| false, |
| }, |
| } |
| |
| for _, test := range tests { |
| t.Run(test.name, func(t *testing.T) { |
| stdout, stderr, err := RunCommand(test.prog, test.args...) |
| if err == nil { |
| if test.expectErr { |
| t.Fatalf("got stdout: %s and stderr: %s, want error", |
| string(stdout), |
| string(stderr)) |
| } |
| |
| if test.expectStdout != string(stdout) { |
| t.Errorf("got stdout %s, want %s", string(stdout), test.expectStdout) |
| } |
| |
| if test.expectStderr != string(stderr) { |
| t.Errorf("got stderr %s, want %s", string(stderr), test.expectStderr) |
| } |
| } else if !test.expectErr { |
| t.Errorf("got unexpected error %v", err) |
| } |
| }) |
| } |
| } |
| |
| // TestFileExists test that the FileExists correctly identifies if a file |
| // exists or not. |
| func TestFileExists(t *testing.T) { |
| // Create a temp file. |
| f, err := ioutil.TempFile(".", "policy_manager-sysapi-test") |
| if err != nil { |
| t.Fatal(err) |
| } |
| |
| // Verify that the temp file exists. |
| if !FileExists(f.Name()) { |
| t.Errorf("Got %s doesn't exists, expect to exist", f.Name()) |
| } |
| |
| // Remove the temp file. |
| if err := os.Remove(f.Name()); err != nil { |
| t.Error(err) |
| } |
| |
| // Verify that a non-existent file doesn't exist. |
| if FileExists("/var/ewoif/feiwjfij") { |
| t.Error("A non-existent file is reported as existing") |
| } |
| } |