github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/hooks/0.1.0/hook_test.go (about)

     1  package hook
     2  
     3  import (
     4  	"testing"
     5  
     6  	current "github.com/containers/libpod/pkg/hooks/1.0.0"
     7  	rspec "github.com/opencontainers/runtime-spec/specs-go"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestGood(t *testing.T) {
    12  	hook, err := read([]byte("{\"hook\": \"/a/b/c\", \"stages\": [\"prestart\"], \"cmds\": [\"sh\"]}"))
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	assert.Equal(t, &current.Hook{
    17  		Version: current.Version,
    18  		Hook: rspec.Hook{
    19  			Path: "/a/b/c",
    20  		},
    21  		When: current.When{
    22  			Commands: []string{"sh"},
    23  			Or:       true,
    24  		},
    25  		Stages: []string{"prestart"},
    26  	}, hook)
    27  }
    28  
    29  func TestInvalidJSON(t *testing.T) {
    30  	_, err := read([]byte("{"))
    31  	if err == nil {
    32  		t.Fatal("unexpected success")
    33  	}
    34  	assert.Regexp(t, "^unexpected end of JSON input$", err.Error())
    35  }
    36  
    37  func TestArguments(t *testing.T) {
    38  	hook, err := read([]byte("{\"hook\": \"/a/b/c\", \"arguments\": [\"d\", \"e\"], \"stages\": [\"prestart\"], \"cmds\": [\"sh\"]}"))
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	assert.Equal(t, &current.Hook{
    43  		Version: current.Version,
    44  		Hook: rspec.Hook{
    45  			Path: "/a/b/c",
    46  			Args: []string{"/a/b/c", "d", "e"},
    47  		},
    48  		When: current.When{
    49  			Commands: []string{"sh"},
    50  			Or:       true,
    51  		},
    52  		Stages: []string{"prestart"},
    53  	}, hook)
    54  }
    55  
    56  func TestEmptyObject(t *testing.T) {
    57  	_, err := read([]byte("{}"))
    58  	if err == nil {
    59  		t.Fatal("unexpected success")
    60  	}
    61  	assert.Regexp(t, "^missing required property: hook$", err.Error())
    62  }
    63  
    64  func TestNoStages(t *testing.T) {
    65  	_, err := read([]byte("{\"hook\": \"/a/b/c\"}"))
    66  	if err == nil {
    67  		t.Fatal("unexpected success")
    68  	}
    69  	assert.Regexp(t, "^missing required property: stages$", err.Error())
    70  }
    71  
    72  func TestStage(t *testing.T) {
    73  	hook, err := read([]byte("{\"hook\": \"/a/b/c\", \"stage\": [\"prestart\"]}"))
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	assert.Equal(t, &current.Hook{
    78  		Version: current.Version,
    79  		Hook: rspec.Hook{
    80  			Path: "/a/b/c",
    81  		},
    82  		When:   current.When{Or: true},
    83  		Stages: []string{"prestart"},
    84  	}, hook)
    85  }
    86  
    87  func TestStagesAndStage(t *testing.T) {
    88  	_, err := read([]byte("{\"hook\": \"/a/b/c\", \"stages\": [\"prestart\"], \"stage\": [\"prestart\"]}"))
    89  	if err == nil {
    90  		t.Fatal("unexpected success")
    91  	}
    92  	assert.Regexp(t, "^cannot set both 'stage' and 'stages'$", err.Error())
    93  }
    94  
    95  func TestCmd(t *testing.T) {
    96  	hook, err := read([]byte("{\"hook\": \"/a/b/c\", \"stage\": [\"prestart\"], \"cmd\": [\"sh\"]}"))
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	assert.Equal(t, &current.Hook{
   101  		Version: current.Version,
   102  		Hook: rspec.Hook{
   103  			Path: "/a/b/c",
   104  		},
   105  		When: current.When{
   106  			Commands: []string{"sh"},
   107  			Or:       true,
   108  		},
   109  		Stages: []string{"prestart"},
   110  	}, hook)
   111  }
   112  
   113  func TestCmdsAndCmd(t *testing.T) {
   114  	_, err := read([]byte("{\"hook\": \"/a/b/c\", \"stages\": [\"prestart\"], \"cmds\": [\"sh\"], \"cmd\": [\"true\"]}"))
   115  	if err == nil {
   116  		t.Fatal("unexpected success")
   117  	}
   118  	assert.Regexp(t, "^cannot set both 'cmd' and 'cmds'$", err.Error())
   119  }
   120  
   121  func TestAnnotations(t *testing.T) {
   122  	hook, err := read([]byte("{\"hook\": \"/a/b/c\", \"stage\": [\"prestart\"], \"annotations\": [\"a\", \"b\"]}"))
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	assert.Equal(t, &current.Hook{
   127  		Version: current.Version,
   128  		Hook: rspec.Hook{
   129  			Path: "/a/b/c",
   130  		},
   131  		When: current.When{
   132  			Annotations: map[string]string{".*": "a|b"},
   133  			Or:          true,
   134  		},
   135  		Stages: []string{"prestart"},
   136  	}, hook)
   137  }
   138  
   139  func TestAnnotation(t *testing.T) {
   140  	hook, err := read([]byte("{\"hook\": \"/a/b/c\", \"stage\": [\"prestart\"], \"annotation\": [\"a\", \"b\"]}"))
   141  	if err != nil {
   142  		t.Fatal(err)
   143  	}
   144  	assert.Equal(t, &current.Hook{
   145  		Version: current.Version,
   146  		Hook: rspec.Hook{
   147  			Path: "/a/b/c",
   148  		},
   149  		When: current.When{
   150  			Annotations: map[string]string{".*": "a|b"},
   151  			Or:          true,
   152  		},
   153  		Stages: []string{"prestart"},
   154  	}, hook)
   155  }
   156  
   157  func TestAnnotationsAndAnnotation(t *testing.T) {
   158  	_, err := read([]byte("{\"hook\": \"/a/b/c\", \"stages\": [\"prestart\"], \"annotations\": [\"a\"], \"annotation\": [\"b\"]}"))
   159  	if err == nil {
   160  		t.Fatal("unexpected success")
   161  	}
   162  	assert.Regexp(t, "^cannot set both 'annotation' and 'annotations'$", err.Error())
   163  }
   164  
   165  func TestHasBindMounts(t *testing.T) {
   166  	hook, err := read([]byte("{\"hook\": \"/a/b/c\", \"stage\": [\"prestart\"], \"hasbindmounts\": true}"))
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  	hasBindMounts := true
   171  	assert.Equal(t, &current.Hook{
   172  		Version: current.Version,
   173  		Hook: rspec.Hook{
   174  			Path: "/a/b/c",
   175  		},
   176  		When: current.When{
   177  			HasBindMounts: &hasBindMounts,
   178  			Or:            true,
   179  		},
   180  		Stages: []string{"prestart"},
   181  	}, hook)
   182  }