gitee.com/mysnapcore/mysnapd@v0.1.0/snapdenv/snapdenv_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package snapdenv_test
    21  
    22  import (
    23  	"os"
    24  	"testing"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"gitee.com/mysnapcore/mysnapd/snapdenv"
    29  )
    30  
    31  func Test(t *testing.T) { TestingT(t) }
    32  
    33  type snapdenvSuite struct{}
    34  
    35  var _ = Suite(&snapdenvSuite{})
    36  
    37  func (s *snapdenvSuite) TestTesting(c *C) {
    38  	oldTesting := os.Getenv("SNAPPY_TESTING")
    39  	defer func() {
    40  		if oldTesting == "" {
    41  			os.Unsetenv("SNAPPY_TESTING")
    42  		} else {
    43  			os.Setenv("SNAPPY_TESTING", oldTesting)
    44  		}
    45  	}()
    46  
    47  	os.Setenv("SNAPPY_TESTING", "1")
    48  	c.Check(snapdenv.Testing(), Equals, true)
    49  
    50  	os.Unsetenv("SNAPPY_TESTING")
    51  	c.Check(snapdenv.Testing(), Equals, false)
    52  }
    53  
    54  func (s *snapdenvSuite) TestMockTesting(c *C) {
    55  	oldTesting := os.Getenv("SNAPPY_TESTING")
    56  	defer func() {
    57  		if oldTesting == "" {
    58  			os.Unsetenv("SNAPPY_TESTING")
    59  		} else {
    60  			os.Setenv("SNAPPY_TESTING", oldTesting)
    61  		}
    62  	}()
    63  	os.Unsetenv("SNAPPY_TESTING")
    64  
    65  	r := snapdenv.MockTesting(true)
    66  	defer r()
    67  
    68  	c.Check(snapdenv.Testing(), Equals, true)
    69  
    70  	snapdenv.MockTesting(false)
    71  	c.Check(snapdenv.Testing(), Equals, false)
    72  }
    73  
    74  func (s *snapdenvSuite) TestUseStagingStore(c *C) {
    75  	oldUseStagingStore := os.Getenv("SNAPPY_USE_STAGING_STORE")
    76  	defer func() {
    77  		if oldUseStagingStore == "" {
    78  			os.Unsetenv("SNAPPY_USE_STAGING_STORE")
    79  		} else {
    80  			os.Setenv("SNAPPY_USE_STAGING_STORE", oldUseStagingStore)
    81  		}
    82  	}()
    83  
    84  	os.Setenv("SNAPPY_USE_STAGING_STORE", "1")
    85  	c.Check(snapdenv.UseStagingStore(), Equals, true)
    86  
    87  	os.Unsetenv("SNAPPY_USE_STAGING_STORE")
    88  	c.Check(snapdenv.UseStagingStore(), Equals, false)
    89  }
    90  
    91  func (s *snapdenvSuite) TestMockUseStagingStore(c *C) {
    92  	oldUseStagingStore := os.Getenv("SNAPPY_USE_STAGING_STORE")
    93  	defer func() {
    94  		if oldUseStagingStore == "" {
    95  			os.Unsetenv("SNAPPY_USE_STAGING_STORE")
    96  		} else {
    97  			os.Setenv("SNAPPY_USE_STAGING_STORE", oldUseStagingStore)
    98  		}
    99  	}()
   100  	os.Unsetenv("SNAPPY_USE_STAGING_STORE")
   101  
   102  	r := snapdenv.MockUseStagingStore(true)
   103  	defer r()
   104  
   105  	c.Check(snapdenv.UseStagingStore(), Equals, true)
   106  
   107  	snapdenv.MockUseStagingStore(false)
   108  	c.Check(snapdenv.UseStagingStore(), Equals, false)
   109  }
   110  
   111  func (s *snapdenvSuite) TestPreseeding(c *C) {
   112  	oldPreseeding := os.Getenv("SNAPD_PRESEED")
   113  	defer func() {
   114  		if oldPreseeding == "" {
   115  			os.Unsetenv("SNAPD_PRESEED")
   116  		} else {
   117  			os.Setenv("SNAPD_PRESEED", oldPreseeding)
   118  		}
   119  	}()
   120  
   121  	os.Setenv("SNAPD_PRESEED", "1")
   122  	c.Check(snapdenv.Preseeding(), Equals, true)
   123  
   124  	os.Unsetenv("SNAPD_PRESEED")
   125  	c.Check(snapdenv.Preseeding(), Equals, false)
   126  }
   127  
   128  func (s *snapdenvSuite) TestMockPreseeding(c *C) {
   129  	oldPreseeding := os.Getenv("SNAPD_PRESEED")
   130  	defer func() {
   131  		if oldPreseeding == "" {
   132  			os.Unsetenv("SNAPD_PRESEED")
   133  		} else {
   134  			os.Setenv("SNAPD_PRESEED", oldPreseeding)
   135  		}
   136  	}()
   137  	os.Unsetenv("SNAPD_PRESEED")
   138  
   139  	r := snapdenv.MockPreseeding(true)
   140  	defer r()
   141  
   142  	c.Check(snapdenv.Preseeding(), Equals, true)
   143  
   144  	snapdenv.MockPreseeding(false)
   145  	c.Check(snapdenv.Preseeding(), Equals, false)
   146  }