github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/kubernetes/provider/storage/volumes_test.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	core "k8s.io/api/core/v1"
    10  
    11  	"github.com/juju/juju/caas/kubernetes/provider/storage"
    12  	"github.com/juju/juju/testing"
    13  )
    14  
    15  type storageSuite struct {
    16  	testing.BaseSuite
    17  }
    18  
    19  var _ = gc.Suite(&storageSuite{})
    20  
    21  func (s *storageSuite) TestParseStorageConfig(c *gc.C) {
    22  	cfg, err := storage.ParseStorageConfig(map[string]interface{}{
    23  		"storage-class":       "juju-ebs",
    24  		"storage-provisioner": "ebs",
    25  		"parameters.type":     "gp2",
    26  	})
    27  	c.Assert(err, jc.ErrorIsNil)
    28  	c.Assert(cfg.StorageClass, gc.Equals, "juju-ebs")
    29  	c.Assert(cfg.StorageProvisioner, gc.Equals, "ebs")
    30  	c.Assert(cfg.Parameters, jc.DeepEquals, map[string]string{"type": "gp2"})
    31  }
    32  
    33  func (s *storageSuite) TestGetStorageMode(c *gc.C) {
    34  	type testCase struct {
    35  		attrs map[string]interface{}
    36  		mode  core.PersistentVolumeAccessMode
    37  		err   string
    38  	}
    39  
    40  	for i, t := range []testCase{
    41  		{
    42  			attrs: map[string]interface{}{
    43  				"storage-mode": "RWO",
    44  			},
    45  			mode: core.ReadWriteOnce,
    46  		},
    47  		{
    48  			attrs: map[string]interface{}{
    49  				"storage-mode": "ReadWriteOnce",
    50  			},
    51  			mode: core.ReadWriteOnce,
    52  		},
    53  		{
    54  			attrs: map[string]interface{}{
    55  				"storage-mode": "RWX",
    56  			},
    57  			mode: core.ReadWriteMany,
    58  		},
    59  		{
    60  			attrs: map[string]interface{}{
    61  				"storage-mode": "ReadWriteMany",
    62  			},
    63  			mode: core.ReadWriteMany,
    64  		},
    65  		{
    66  			attrs: map[string]interface{}{
    67  				"storage-mode": "ROX",
    68  			},
    69  			mode: core.ReadOnlyMany,
    70  		},
    71  		{
    72  			attrs: map[string]interface{}{
    73  				"storage-mode": "ReadOnlyMany",
    74  			},
    75  			mode: core.ReadOnlyMany,
    76  		},
    77  		{
    78  			attrs: map[string]interface{}{
    79  				"storage-mode": "bad-mode",
    80  			},
    81  			err: `storage mode "bad-mode" not supported`,
    82  		},
    83  	} {
    84  		c.Logf("testing get storage mode %d", i)
    85  		mode, err := storage.ParseStorageMode(t.attrs)
    86  		if t.err == "" {
    87  			c.Check(err, jc.ErrorIsNil)
    88  			c.Check(*mode, jc.DeepEquals, t.mode)
    89  		} else {
    90  			c.Check(err, gc.ErrorMatches, t.err)
    91  		}
    92  	}
    93  }
    94  
    95  func (s *storageSuite) TestPushUniqueVolume(c *gc.C) {
    96  	podSpec := &core.PodSpec{}
    97  
    98  	vol1 := core.Volume{
    99  		Name: "vol1",
   100  		VolumeSource: core.VolumeSource{
   101  			EmptyDir: &core.EmptyDirVolumeSource{},
   102  		},
   103  	}
   104  	vol2 := core.Volume{
   105  		Name: "vol2",
   106  		VolumeSource: core.VolumeSource{
   107  			HostPath: &core.HostPathVolumeSource{
   108  				Path: "/var/log/gitlab",
   109  			},
   110  		},
   111  	}
   112  	aDifferentVol2 := core.Volume{
   113  		Name: "vol2",
   114  		VolumeSource: core.VolumeSource{
   115  			HostPath: &core.HostPathVolumeSource{
   116  				Path: "/var/log/foo",
   117  			},
   118  		},
   119  	}
   120  	err := storage.PushUniqueVolume(podSpec, vol1, false)
   121  	c.Assert(err, jc.ErrorIsNil)
   122  	c.Assert(podSpec.Volumes, jc.DeepEquals, []core.Volume{
   123  		vol1,
   124  	})
   125  
   126  	err = storage.PushUniqueVolume(podSpec, vol1, false)
   127  	c.Assert(err, jc.ErrorIsNil)
   128  	c.Assert(podSpec.Volumes, jc.DeepEquals, []core.Volume{
   129  		vol1,
   130  	})
   131  
   132  	err = storage.PushUniqueVolume(podSpec, vol2, false)
   133  	c.Assert(err, jc.ErrorIsNil)
   134  	c.Assert(podSpec.Volumes, jc.DeepEquals, []core.Volume{
   135  		vol1, vol2,
   136  	})
   137  
   138  	err = storage.PushUniqueVolume(podSpec, aDifferentVol2, false)
   139  	c.Assert(err, gc.ErrorMatches, `duplicated volume "vol2" not valid`)
   140  	c.Assert(podSpec.Volumes, jc.DeepEquals, []core.Volume{
   141  		vol1, vol2,
   142  	})
   143  
   144  	err = storage.PushUniqueVolume(podSpec, aDifferentVol2, true)
   145  	c.Assert(err, jc.ErrorIsNil)
   146  	c.Assert(podSpec.Volumes, jc.DeepEquals, []core.Volume{
   147  		vol1, aDifferentVol2,
   148  	})
   149  }