github.com/apptainer/singularity@v3.1.1+incompatible/etc/conf/gen_test.go (about) 1 // Copyright (c) 2018, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package main 7 8 import ( 9 "bytes" 10 "io/ioutil" 11 "os" 12 "testing" 13 14 "github.com/sylabs/singularity/internal/pkg/test" 15 ) 16 17 func TestGenConf(t *testing.T) { 18 tmpl := "testdata/test_default.tmpl" 19 var testerUID, testerGid int 20 files := []string{"testdata/test_2.in", "testdata/test_3.in"} 21 22 for _, conf := range files { 23 // Config file must be root owned 24 err := os.Chown(conf, 0, 0) 25 if err != nil { 26 t.Fatal(err) 27 } 28 } 29 30 tests := []struct { 31 name string 32 confInPath string 33 confOutPath string 34 confCorrectPath string 35 }{ 36 {"gen_new", "", "testdata/test_1.out", "testdata/test_1.out.correct"}, 37 {"gen_update", "testdata/test_2.in", "testdata/test_2.out", "testdata/test_2.out.correct"}, 38 {"gen_update_newvals", "testdata/test_3.in", "testdata/test_3.out", "testdata/test_3.out.correct"}, 39 } 40 41 for _, tt := range tests { 42 t.Run(tt.name, test.WithoutPrivilege(func(t *testing.T) { 43 testerUID = os.Getuid() 44 testerGid = os.Getgid() 45 46 defer os.Remove(tt.confOutPath) 47 48 genConf(tmpl, tt.confInPath, tt.confOutPath) 49 50 if eq, err := compareFile(tt.confOutPath, tt.confCorrectPath); err != nil { 51 t.Fatalf("Unable to compare files: %v\n", err) 52 } else if !eq { 53 t.Fatalf("Output file %v does not match correct output %v\n", tt.confOutPath, tt.confCorrectPath) 54 } 55 })) 56 } 57 58 // Return files to default owner 59 for _, conf := range files { 60 err := os.Chown(conf, testerUID, testerGid) 61 if err != nil { 62 t.Fatal(err) 63 } 64 } 65 } 66 67 func compareFile(p1, p2 string) (bool, error) { 68 f1, err := ioutil.ReadFile(p1) 69 if err != nil { 70 return false, err 71 } 72 73 f2, err := ioutil.ReadFile(p2) 74 if err != nil { 75 return false, err 76 } 77 78 return bytes.Equal(f1, f2), nil 79 }