...

Source file src/github.com/coreos/go-systemd/v22/machine1/dbus_test.go

Documentation: github.com/coreos/go-systemd/v22/machine1

     1  /*
     2  Copyright 2015 CoreOS Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package machine1
    18  
    19  import (
    20  	"fmt"
    21  	"math/rand"
    22  	"os"
    23  	"os/exec"
    24  	"path/filepath"
    25  	"testing"
    26  	"time"
    27  
    28  	sd_dbus "github.com/coreos/go-systemd/v22/dbus"
    29  	"github.com/godbus/dbus/v5"
    30  )
    31  
    32  const (
    33  	machinePrefix = "machined-test-"
    34  )
    35  
    36  func mustCreateTestProcess(machineName string) (pid int) {
    37  	testServiceName := machineName + ".service"
    38  	systemdRun, err := exec.LookPath("systemd-run")
    39  	if err != nil {
    40  		panic(err.Error())
    41  	}
    42  	sleep, err := exec.LookPath("sleep")
    43  	if err != nil {
    44  		panic(err.Error())
    45  	}
    46  	cmd := exec.Command(systemdRun, "--unit="+testServiceName, sleep, "5000")
    47  	out, err := cmd.CombinedOutput()
    48  	if err != nil {
    49  		panic(fmt.Errorf("systemd-run failed: %q", out))
    50  	}
    51  	dbusConn, err := sd_dbus.New()
    52  	if err != nil {
    53  		panic(err.Error())
    54  	}
    55  	defer dbusConn.Close()
    56  	mainPIDProperty, err := dbusConn.GetServiceProperty(testServiceName, "MainPID")
    57  	if err != nil {
    58  		panic(err.Error())
    59  	}
    60  	mainPID := mainPIDProperty.Value.Value().(uint32)
    61  	return int(mainPID)
    62  }
    63  
    64  func TestMachine(t *testing.T) {
    65  	machineNames := []string{
    66  		machinePrefix + "register-" + generateRandomLabel(8),
    67  		machinePrefix + "register-with-network-" + generateRandomLabel(8),
    68  		machinePrefix + "create-" + generateRandomLabel(8),
    69  		machinePrefix + "create-with-network-" + generateRandomLabel(8),
    70  	}
    71  	leaders := []int{
    72  		mustCreateTestProcess(machineNames[0]),
    73  		mustCreateTestProcess(machineNames[1]),
    74  		mustCreateTestProcess(machineNames[2]),
    75  		mustCreateTestProcess(machineNames[3]),
    76  	}
    77  
    78  	conn, newErr := New()
    79  	if newErr != nil {
    80  		t.Fatal(newErr)
    81  	}
    82  
    83  	regErr := conn.RegisterMachine(machineNames[0], nil, "go-systemd", "container", leaders[0], "")
    84  	if regErr != nil {
    85  		t.Fatal(regErr)
    86  	}
    87  
    88  	regWithNetworkErr := conn.RegisterMachineWithNetwork(machineNames[1], nil, "go-systemd", "container", leaders[1], "", nil)
    89  	if regWithNetworkErr != nil {
    90  		t.Fatal(regWithNetworkErr)
    91  	}
    92  
    93  	createErr := conn.CreateMachine(machineNames[2], nil, "go-systemd", "container", leaders[2], "", nil)
    94  	if createErr != nil {
    95  		t.Fatal(createErr)
    96  	}
    97  
    98  	createWithNetworkErr := conn.CreateMachineWithNetwork(machineNames[3], nil, "go-systemd", "container", leaders[3], "", nil, nil)
    99  	if createWithNetworkErr != nil {
   100  		t.Fatal(createWithNetworkErr)
   101  	}
   102  
   103  	machines := make([]dbus.ObjectPath, 0)
   104  	for _, v := range machineNames {
   105  		machine, getErr := conn.GetMachine(v)
   106  		if getErr != nil {
   107  			t.Fatal(getErr)
   108  		}
   109  		if machine != "" {
   110  			machines = append(machines, machine)
   111  		}
   112  	}
   113  	if len(machines) != 4 {
   114  		t.Fatalf("did not find all machine nameds %s", machineNames)
   115  	}
   116  
   117  	listMachines, getErr := conn.ListMachines()
   118  	if getErr != nil {
   119  		t.Fatal(getErr)
   120  	}
   121  
   122  	// listMachines includes also `.host`, so by default the length should be greater than 2
   123  	if len(listMachines) <= 4 {
   124  		t.Fatalf("did not find any machine")
   125  	}
   126  
   127  	for _, v := range machineNames {
   128  		tErr := conn.TerminateMachine(v)
   129  		if tErr != nil {
   130  			t.Fatal(tErr)
   131  		}
   132  		var machine dbus.ObjectPath
   133  		for i := 1; i <= 10; i++ {
   134  			machine, getErr = conn.GetMachine(v)
   135  			if len(machine) == 0 && getErr != nil {
   136  				break
   137  			}
   138  			time.Sleep(1 * time.Second)
   139  		}
   140  		if len(machine) != 0 {
   141  			t.Fatalf("unexpectedly found machine named %s", v)
   142  		} else if getErr == nil {
   143  			t.Fatal("expected error but got nil")
   144  		}
   145  	}
   146  }
   147  
   148  func generateRandomLabel(n int) string {
   149  	letters := []rune("abcdefghijklmnopqrstuvwxyz")
   150  	s := make([]rune, n)
   151  	rand.Seed(time.Now().UnixNano())
   152  	for i := range s {
   153  		s[i] = letters[rand.Intn(len(letters))]
   154  	}
   155  	return string(s)
   156  }
   157  
   158  func TestImages(t *testing.T) {
   159  	imageName := machinePrefix + generateRandomLabel(8)
   160  	imagePath := filepath.Join("/var/lib/machines", imageName)
   161  
   162  	if _, err := os.Create(imagePath); err != nil {
   163  		t.Fatal(err)
   164  	}
   165  	defer os.Remove(imagePath)
   166  
   167  	if err := os.Truncate(imagePath, 500*1024*1024); err != nil {
   168  		t.Fatal(err)
   169  	}
   170  
   171  	conn, newErr := New()
   172  	if newErr != nil {
   173  		t.Fatal(newErr)
   174  	}
   175  
   176  	listImages, listErr := conn.ListImages()
   177  	if listErr != nil {
   178  		t.Fatal(listErr)
   179  	}
   180  
   181  	if len(listImages) < 1 {
   182  		t.Fatalf("did not find any image")
   183  	}
   184  }
   185  

View as plain text