github.com/wtsi-ssg/wrstat@v1.1.4-0.20221008232152-3030622a8cf8/ch/from_test.go (about)

     1  /*******************************************************************************
     2   * Copyright (c) 2021 Genome Research Ltd.
     3   *
     4   * Author: Sendu Bala <sb10@sanger.ac.uk>
     5   *
     6   * Permission is hereby granted, free of charge, to any person obtaining
     7   * a copy of this software and associated documentation files (the
     8   * "Software"), to deal in the Software without restriction, including
     9   * without limitation the rights to use, copy, modify, merge, publish,
    10   * distribute, sublicense, and/or sell copies of the Software, and to
    11   * permit persons to whom the Software is furnished to do so, subject to
    12   * the following conditions:
    13   *
    14   * The above copyright notice and this permission notice shall be included
    15   * in all copies or substantial portions of the Software.
    16   *
    17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    18   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    19   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    20   * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    21   * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    22   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    23   * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    24   ******************************************************************************/
    25  
    26  package ch
    27  
    28  import (
    29  	"bytes"
    30  	"fmt"
    31  	"testing"
    32  
    33  	. "github.com/smartystreets/goconvey/convey"
    34  )
    35  
    36  func TestGIDFromSubDir(t *testing.T) {
    37  	primaryGID, otherGIDs := getGIDs(t)
    38  
    39  	if len(otherGIDs) == 0 {
    40  		SkipConvey("Can't test GIDFromSubDir since you don't belong to multiple groups", t, func() {})
    41  
    42  		return
    43  	}
    44  
    45  	primaryName := testGroupName(t, primaryGID)
    46  	otherName := testGroupName(t, otherGIDs[0])
    47  
    48  	Convey("Given a GIDFromSubDir's PathChecker", t, func() {
    49  		buff, l := newLogger()
    50  
    51  		f, err := NewGIDFromSubDir(
    52  			[]string{"/disk1", "/disk2/sub", "/disk3"},
    53  			"teams",
    54  			map[string]string{"foo": otherName},
    55  			"projects",
    56  			map[string]int{primaryName: otherGIDs[0]},
    57  			l,
    58  		)
    59  		So(err, ShouldBeNil)
    60  		So(f, ShouldNotBeNil)
    61  
    62  		p := f.PathChecker()
    63  		So(p, ShouldNotBeNil)
    64  
    65  		testPaths(p, otherGIDs[0], primaryName, otherName, buff)
    66  	})
    67  
    68  	Convey("NewGIDFromSubDir fails with a bad lookup", t, func() {
    69  		_, l := newLogger()
    70  
    71  		f, err := NewGIDFromSubDir(
    72  			[]string{"/disk1", "/disk2/sub", "/disk3"},
    73  			"teams",
    74  			map[string]string{"foo": "!@£$"},
    75  			"projects",
    76  			map[string]int{primaryName: otherGIDs[0]},
    77  			l,
    78  		)
    79  		So(err, ShouldNotBeNil)
    80  		So(f, ShouldBeNil)
    81  	})
    82  
    83  	Convey("You can create a GIDFromSubDir from YAML", t, func() {
    84  		buff, l := newLogger()
    85  
    86  		data := `
    87  prefixes: ["/disk1", "/disk2/sub", "/disk3"]
    88  lookupDir: teams
    89  lookup:
    90    foo: ` + otherName + `
    91  directDir: projects
    92  exceptions:
    93    ` + fmt.Sprintf("%s: %d\n", primaryName, otherGIDs[0])
    94  
    95  		f, err := NewGIDFromSubDirFromYAML([]byte(data), l)
    96  		So(err, ShouldBeNil)
    97  		So(f, ShouldNotBeNil)
    98  
    99  		p := f.PathChecker()
   100  		So(p, ShouldNotBeNil)
   101  
   102  		testPaths(p, otherGIDs[0], primaryName, otherName, buff)
   103  
   104  		Convey("You don't have to specify exceptions", func() {
   105  			data := `
   106  prefixes: ["/disk1", "/disk2/sub", "/disk3"]
   107  lookupDir: teams
   108  lookup:
   109    foo: ` + otherName + `
   110  directDir: projects
   111  `
   112  
   113  			f, err := NewGIDFromSubDirFromYAML([]byte(data), l)
   114  			So(err, ShouldBeNil)
   115  			So(f, ShouldNotBeNil)
   116  
   117  			p := f.PathChecker()
   118  			So(p, ShouldNotBeNil)
   119  
   120  			ok, gid := p("/disk1/projects/foo/file2.txt")
   121  			So(ok, ShouldBeFalse)
   122  			So(gid, ShouldEqual, badUnixGroup)
   123  			So(buff.String(), ShouldContainSubstring, "subdir not a unix group name")
   124  		})
   125  	})
   126  
   127  	Convey("You can't create a GIDFromSubDir from invalid YAML", t, func() {
   128  		_, l := newLogger()
   129  
   130  		data := `
   131  prefixes: ["/disk1", "/disk2/sub", "/disk3"}
   132  lookupDir: teams
   133  `
   134  
   135  		f, err := NewGIDFromSubDirFromYAML([]byte(data), l)
   136  		So(err, ShouldNotBeNil)
   137  		So(f, ShouldBeNil)
   138  
   139  		data = `prefixes: ["/disk1", "/disk2/sub", "/disk3"]`
   140  
   141  		f, err = NewGIDFromSubDirFromYAML([]byte(data), l)
   142  		So(err, ShouldNotBeNil)
   143  		So(err, ShouldEqual, errInvalidYAML)
   144  		So(f, ShouldBeNil)
   145  	})
   146  }
   147  
   148  // testPaths tests that the PathChecker behaves as expected.
   149  func testPaths(p PathChecker, expectedGID int, expectedName1, expectedName2 string, buff *bytes.Buffer) {
   150  	Convey("Valid paths return GIDs", func() {
   151  		ok, gid := p("/disk1/teams/foo/file1.txt")
   152  		So(ok, ShouldBeTrue)
   153  		So(gid, ShouldEqual, expectedGID)
   154  
   155  		ok, gid = p("/disk1/projects/" + expectedName1 + "/file2.txt")
   156  		So(ok, ShouldBeTrue)
   157  		So(gid, ShouldEqual, expectedGID)
   158  
   159  		ok, gid = p("/disk1/projects/" + expectedName2 + "/file2.txt")
   160  		So(ok, ShouldBeTrue)
   161  		So(gid, ShouldEqual, expectedGID)
   162  	})
   163  
   164  	Convey("Invalid paths return false and log errors", func() {
   165  		ok, gid := p("/disk3/file4.txt")
   166  		So(ok, ShouldBeFalse)
   167  		So(gid, ShouldEqual, 0)
   168  		So(buff.String(), ShouldBeBlank)
   169  
   170  		ok, gid = p("/disk1/teams/bar/file1.txt")
   171  		So(ok, ShouldBeFalse)
   172  		So(gid, ShouldEqual, badUnixGroup)
   173  		So(buff.String(), ShouldContainSubstring, "subdir not in group lookup")
   174  		So(buff.String(), ShouldContainSubstring, "path=/disk1/teams/bar/file1.txt")
   175  		buff.Reset()
   176  
   177  		ok, gid = p("/disk1/projects/bar/file2.txt")
   178  		So(ok, ShouldBeFalse)
   179  		So(gid, ShouldEqual, badUnixGroup)
   180  		So(buff.String(), ShouldContainSubstring, "subdir not a unix group name")
   181  		So(buff.String(), ShouldContainSubstring, "path=/disk1/projects/bar/file2.txt")
   182  	})
   183  }