github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/interfaces/systemd/spec_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 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 systemd_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/interfaces/systemd"
    26  )
    27  
    28  type specSuite struct{}
    29  
    30  var _ = Suite(&specSuite{})
    31  
    32  func (s *specSuite) TestAddService(c *C) {
    33  	spec := systemd.Specification{}
    34  	c.Assert(spec.Services(), IsNil)
    35  	svc1 := &systemd.Service{ExecStart: "one"}
    36  	err := spec.AddService("svc1.service", svc1)
    37  	c.Assert(err, IsNil)
    38  	svc2 := &systemd.Service{ExecStart: "two"}
    39  	err = spec.AddService("svc2.service", svc2)
    40  	c.Assert(err, IsNil)
    41  	c.Assert(spec.Services(), DeepEquals, map[string]*systemd.Service{
    42  		"svc1.service": svc1,
    43  		"svc2.service": svc2,
    44  	})
    45  }
    46  
    47  func (s *specSuite) TestClashing(c *C) {
    48  	svc1 := &systemd.Service{ExecStart: "one"}
    49  	svc2 := &systemd.Service{ExecStart: "two"}
    50  	spec := systemd.Specification{}
    51  	err := spec.AddService("foo.service", svc1)
    52  	c.Assert(err, IsNil)
    53  	err = spec.AddService("foo.service", svc2)
    54  	c.Assert(err, ErrorMatches, `interface requires conflicting system needs: service "foo.service" used to be defined as .*, now re-defined as .*`)
    55  }
    56  
    57  func (s *specSuite) TestDifferentObjectsNotClashing(c *C) {
    58  	svc1 := &systemd.Service{ExecStart: "one and the same"}
    59  	svc2 := &systemd.Service{ExecStart: "one and the same"}
    60  	spec := systemd.Specification{}
    61  	err := spec.AddService("foo.service", svc1)
    62  	c.Assert(err, IsNil)
    63  	err = spec.AddService("foo.service", svc2)
    64  	c.Assert(err, IsNil)
    65  }