123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- // Copyright 2015 CoreOS, Inc.
- //
- // 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.
- package system
- import (
- "io/ioutil"
- "os"
- "path"
- "strings"
- "syscall"
- "testing"
- "github.com/rancher/os/config/cloudinit/config"
- )
- const (
- base = "# a file\nFOO=base\n\nBAR= hi there\n"
- baseNoNewline = "# a file\nFOO=base\n\nBAR= hi there"
- baseDos = "# a file\r\nFOO=base\r\n\r\nBAR= hi there\r\n"
- expectUpdate = "# a file\nFOO=test\n\nBAR= hi there\nNEW=a value\n"
- expectCreate = "FOO=test\nNEW=a value\n"
- )
- var (
- valueUpdate = map[string]string{
- "FOO": "test",
- "NEW": "a value",
- }
- valueNoop = map[string]string{
- "FOO": "base",
- }
- valueEmpty = map[string]string{}
- valueInvalid = map[string]string{
- "FOO-X": "test",
- }
- )
- func TestWriteEnvFileUpdate(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- fullPath := path.Join(dir, name)
- ioutil.WriteFile(fullPath, []byte(base), 0644)
- oldStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueUpdate,
- }
- err = WriteEnvFile(&ef, dir)
- if err != nil {
- t.Fatalf("WriteFile failed: %v", err)
- }
- contents, err := ioutil.ReadFile(fullPath)
- if err != nil {
- t.Fatalf("Unable to read expected file: %v", err)
- }
- if string(contents) != expectUpdate {
- t.Fatalf("File has incorrect contents: %q", contents)
- }
- newStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino {
- t.Fatalf("File was not replaced: %s", fullPath)
- }
- }
- func TestWriteEnvFileUpdateNoNewline(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- fullPath := path.Join(dir, name)
- ioutil.WriteFile(fullPath, []byte(baseNoNewline), 0644)
- oldStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueUpdate,
- }
- err = WriteEnvFile(&ef, dir)
- if err != nil {
- t.Fatalf("WriteFile failed: %v", err)
- }
- contents, err := ioutil.ReadFile(fullPath)
- if err != nil {
- t.Fatalf("Unable to read expected file: %v", err)
- }
- if string(contents) != expectUpdate {
- t.Fatalf("File has incorrect contents: %q", contents)
- }
- newStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino {
- t.Fatalf("File was not replaced: %s", fullPath)
- }
- }
- func TestWriteEnvFileCreate(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- fullPath := path.Join(dir, name)
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueUpdate,
- }
- err = WriteEnvFile(&ef, dir)
- if err != nil {
- t.Fatalf("WriteFile failed: %v", err)
- }
- contents, err := ioutil.ReadFile(fullPath)
- if err != nil {
- t.Fatalf("Unable to read expected file: %v", err)
- }
- if string(contents) != expectCreate {
- t.Fatalf("File has incorrect contents: %q", contents)
- }
- }
- func TestWriteEnvFileNoop(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- fullPath := path.Join(dir, name)
- ioutil.WriteFile(fullPath, []byte(base), 0644)
- oldStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueNoop,
- }
- err = WriteEnvFile(&ef, dir)
- if err != nil {
- t.Fatalf("WriteFile failed: %v", err)
- }
- contents, err := ioutil.ReadFile(fullPath)
- if err != nil {
- t.Fatalf("Unable to read expected file: %v", err)
- }
- if string(contents) != base {
- t.Fatalf("File has incorrect contents: %q", contents)
- }
- newStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- if oldStat.Sys().(*syscall.Stat_t).Ino != newStat.Sys().(*syscall.Stat_t).Ino {
- t.Fatalf("File was replaced: %s", fullPath)
- }
- }
- func TestWriteEnvFileUpdateDos(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- fullPath := path.Join(dir, name)
- ioutil.WriteFile(fullPath, []byte(baseDos), 0644)
- oldStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueUpdate,
- }
- err = WriteEnvFile(&ef, dir)
- if err != nil {
- t.Fatalf("WriteFile failed: %v", err)
- }
- contents, err := ioutil.ReadFile(fullPath)
- if err != nil {
- t.Fatalf("Unable to read expected file: %v", err)
- }
- if string(contents) != expectUpdate {
- t.Fatalf("File has incorrect contents: %q", contents)
- }
- newStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino {
- t.Fatalf("File was not replaced: %s", fullPath)
- }
- }
- // A middle ground noop, values are unchanged but we did have a value.
- // Seems reasonable to rewrite the file in Unix format anyway.
- func TestWriteEnvFileDos2Unix(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- fullPath := path.Join(dir, name)
- ioutil.WriteFile(fullPath, []byte(baseDos), 0644)
- oldStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueNoop,
- }
- err = WriteEnvFile(&ef, dir)
- if err != nil {
- t.Fatalf("WriteFile failed: %v", err)
- }
- contents, err := ioutil.ReadFile(fullPath)
- if err != nil {
- t.Fatalf("Unable to read expected file: %v", err)
- }
- if string(contents) != base {
- t.Fatalf("File has incorrect contents: %q", contents)
- }
- newStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino {
- t.Fatalf("File was not replaced: %s", fullPath)
- }
- }
- // If it really is a noop (structure is empty) don't even do dos2unix
- func TestWriteEnvFileEmpty(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- fullPath := path.Join(dir, name)
- ioutil.WriteFile(fullPath, []byte(baseDos), 0644)
- oldStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueEmpty,
- }
- err = WriteEnvFile(&ef, dir)
- if err != nil {
- t.Fatalf("WriteFile failed: %v", err)
- }
- contents, err := ioutil.ReadFile(fullPath)
- if err != nil {
- t.Fatalf("Unable to read expected file: %v", err)
- }
- if string(contents) != baseDos {
- t.Fatalf("File has incorrect contents: %q", contents)
- }
- newStat, err := os.Stat(fullPath)
- if err != nil {
- t.Fatalf("Unable to stat file: %v", err)
- }
- if oldStat.Sys().(*syscall.Stat_t).Ino != newStat.Sys().(*syscall.Stat_t).Ino {
- t.Fatalf("File was replaced: %s", fullPath)
- }
- }
- // no point in creating empty files
- func TestWriteEnvFileEmptyNoCreate(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- fullPath := path.Join(dir, name)
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueEmpty,
- }
- err = WriteEnvFile(&ef, dir)
- if err != nil {
- t.Fatalf("WriteFile failed: %v", err)
- }
- contents, err := ioutil.ReadFile(fullPath)
- if err == nil {
- t.Fatalf("File has incorrect contents: %q", contents)
- } else if !os.IsNotExist(err) {
- t.Fatalf("Unexpected error while reading file: %v", err)
- }
- }
- func SvenBrokeTestWriteEnvFilePermFailure(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- fullPath := path.Join(dir, name)
- ioutil.WriteFile(fullPath, []byte(base), 0000)
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueUpdate,
- }
- err = WriteEnvFile(&ef, dir)
- if !os.IsPermission(err) {
- t.Fatalf("Not a pemission denied error: %v", err)
- }
- }
- func TestWriteEnvFileNameFailure(t *testing.T) {
- dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
- if err != nil {
- t.Fatalf("Unable to create tempdir: %v", err)
- }
- defer os.RemoveAll(dir)
- name := "foo.conf"
- ef := EnvFile{
- File: &File{config.File{
- Path: name,
- }},
- Vars: valueInvalid,
- }
- err = WriteEnvFile(&ef, dir)
- if err == nil || !strings.HasPrefix(err.Error(), "Invalid name") {
- t.Fatalf("Not an invalid name error: %v", err)
- }
- }
|