github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/cloud/test/integration/fixtures/device.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     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 fixtures
    18  
    19  import (
    20  	"k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  
    23  	"github.com/kubeedge/kubeedge/cloud/pkg/apis/devices/v1alpha1"
    24  )
    25  
    26  type DeviceOp struct {
    27  	device v1alpha1.Device
    28  }
    29  
    30  type DeviceOption func(*DeviceOp)
    31  
    32  func (op *DeviceOp) applyDeviceOpts(opts []DeviceOption) {
    33  	for _, opt := range opts {
    34  		opt(op)
    35  	}
    36  }
    37  
    38  func newDeviceOp(opts ...DeviceOption) *DeviceOp {
    39  	op := &DeviceOp{
    40  		device: v1alpha1.Device{
    41  			Spec:       v1alpha1.DeviceSpec{},
    42  			ObjectMeta: metav1.ObjectMeta{},
    43  			TypeMeta: metav1.TypeMeta{
    44  				APIVersion: apiVersion,
    45  				Kind:       deviceKind,
    46  			},
    47  		},
    48  	}
    49  	op.applyDeviceOpts(opts)
    50  	return op
    51  }
    52  
    53  func withDeviceName(name string) DeviceOption {
    54  	return func(op *DeviceOp) {
    55  		op.device.ObjectMeta.Name = name
    56  	}
    57  }
    58  
    59  func withDeviceNamespace(namespace string) DeviceOption {
    60  	return func(op *DeviceOp) {
    61  		op.device.ObjectMeta.Namespace = namespace
    62  	}
    63  }
    64  
    65  func withDeviceModelReference(deviceModelRef string) DeviceOption {
    66  	return func(op *DeviceOp) {
    67  		op.device.Spec.DeviceModelRef = &v1.LocalObjectReference{
    68  			Name: deviceModelRef,
    69  		}
    70  	}
    71  }
    72  
    73  func withProtocolConfig(protocol deviceProtocol) DeviceOption {
    74  	return func(op *DeviceOp) {
    75  		switch protocol {
    76  		case deviceProtocolModbusRTU:
    77  			op.device.Spec.Protocol = v1alpha1.ProtocolConfig{
    78  				Modbus: &v1alpha1.ProtocolConfigModbus{
    79  					RTU: &v1alpha1.ProtocolConfigModbusRTU{},
    80  				},
    81  			}
    82  		case deviceProtocolModbusTCP:
    83  			op.device.Spec.Protocol = v1alpha1.ProtocolConfig{
    84  				Modbus: &v1alpha1.ProtocolConfigModbus{
    85  					TCP: &v1alpha1.ProtocolConfigModbusTCP{},
    86  				},
    87  			}
    88  		case deviceProtocolOPCUA:
    89  			op.device.Spec.Protocol = v1alpha1.ProtocolConfig{
    90  				OpcUA: &v1alpha1.ProtocolConfigOpcUA{},
    91  			}
    92  		default:
    93  		}
    94  	}
    95  }
    96  
    97  func withBaudRate(baudRate int64) DeviceOption {
    98  	return func(op *DeviceOp) {
    99  		op.device.Spec.Protocol.Modbus.RTU.BaudRate = baudRate
   100  	}
   101  }
   102  
   103  func withDataBits(dataBits int64) DeviceOption {
   104  	return func(op *DeviceOp) {
   105  		op.device.Spec.Protocol.Modbus.RTU.DataBits = dataBits
   106  	}
   107  }
   108  
   109  func withParity(parity string) DeviceOption {
   110  	return func(op *DeviceOp) {
   111  		op.device.Spec.Protocol.Modbus.RTU.Parity = parity
   112  	}
   113  }
   114  
   115  func withSerialPort(serialPort string) DeviceOption {
   116  	return func(op *DeviceOp) {
   117  		op.device.Spec.Protocol.Modbus.RTU.SerialPort = serialPort
   118  	}
   119  }
   120  
   121  func withStopBits(stopBits int64) DeviceOption {
   122  	return func(op *DeviceOp) {
   123  		op.device.Spec.Protocol.Modbus.RTU.StopBits = stopBits
   124  	}
   125  }
   126  
   127  func withSlaveID(slaveID int64) DeviceOption {
   128  	return func(op *DeviceOp) {
   129  		op.device.Spec.Protocol.Modbus.RTU.SlaveID = slaveID
   130  	}
   131  }
   132  
   133  func withTCPPort(port int64) DeviceOption {
   134  	return func(op *DeviceOp) {
   135  		op.device.Spec.Protocol.Modbus.TCP.Port = port
   136  	}
   137  }
   138  
   139  func withTCPSlaveID(tcpSlaveID string) DeviceOption {
   140  	return func(op *DeviceOp) {
   141  		op.device.Spec.Protocol.Modbus.TCP.SlaveID = tcpSlaveID
   142  	}
   143  }
   144  
   145  func withTCPServerIP(ip string) DeviceOption {
   146  	return func(op *DeviceOp) {
   147  		op.device.Spec.Protocol.Modbus.TCP.IP = ip
   148  	}
   149  }
   150  
   151  func withOPCUAServerURL(url string) DeviceOption {
   152  	return func(op *DeviceOp) {
   153  		op.device.Spec.Protocol.OpcUA.URL = url
   154  	}
   155  }
   156  
   157  func NewDeviceModbusRTU(name string, namespace string) v1alpha1.Device {
   158  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   159  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withDataBits(dataBits), withParity(parity), withSerialPort(serialPort),
   160  		withStopBits(stopBits), withSlaveID(slaveID))
   161  	return deviceInstanceOp.device
   162  }
   163  
   164  func NewDeviceModbusRTUNoBaudRate(name string, namespace string) v1alpha1.Device {
   165  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   166  		withProtocolConfig(deviceProtocolModbusRTU), withDataBits(dataBits), withParity(parity), withSerialPort(serialPort),
   167  		withStopBits(stopBits), withSlaveID(slaveID))
   168  	return deviceInstanceOp.device
   169  }
   170  
   171  func NewDeviceModbusRTUBadBaudRate(name string, namespace string) v1alpha1.Device {
   172  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   173  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(100), withDataBits(dataBits), withParity(parity),
   174  		withSerialPort(serialPort), withStopBits(stopBits), withSlaveID(slaveID))
   175  	return deviceInstanceOp.device
   176  }
   177  
   178  func NewDeviceModbusRTUNoDataBits(name string, namespace string) v1alpha1.Device {
   179  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   180  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withParity(parity), withSerialPort(serialPort),
   181  		withStopBits(stopBits), withSlaveID(slaveID))
   182  	return deviceInstanceOp.device
   183  }
   184  
   185  func NewDeviceModbusRTUBadDataBits(name string, namespace string) v1alpha1.Device {
   186  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   187  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withDataBits(10), withParity(parity),
   188  		withSerialPort(serialPort), withStopBits(stopBits), withSlaveID(slaveID))
   189  	return deviceInstanceOp.device
   190  }
   191  
   192  func NewDeviceModbusRTUNoParity(name string, namespace string) v1alpha1.Device {
   193  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   194  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withDataBits(10), withSerialPort(serialPort),
   195  		withStopBits(stopBits), withSlaveID(slaveID))
   196  	return deviceInstanceOp.device
   197  }
   198  
   199  func NewDeviceModbusRTUBadParity(name string, namespace string) v1alpha1.Device {
   200  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   201  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withDataBits(dataBits), withParity("test"),
   202  		withSerialPort(serialPort), withStopBits(stopBits), withSlaveID(slaveID))
   203  	return deviceInstanceOp.device
   204  }
   205  
   206  func NewDeviceModbusRTUNoSerialPort(name string, namespace string) v1alpha1.Device {
   207  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   208  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withDataBits(dataBits), withParity(parity),
   209  		withStopBits(stopBits), withSlaveID(slaveID))
   210  	return deviceInstanceOp.device
   211  }
   212  
   213  func NewDeviceModbusRTUNoSlaveID(name string, namespace string) v1alpha1.Device {
   214  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   215  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withDataBits(dataBits), withParity(parity),
   216  		withSerialPort(serialPort), withStopBits(stopBits))
   217  	return deviceInstanceOp.device
   218  }
   219  
   220  func NewDeviceModbusRTUBadSlaveID(name string, namespace string) v1alpha1.Device {
   221  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   222  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withDataBits(dataBits), withParity(parity),
   223  		withSerialPort(serialPort), withStopBits(stopBits), withSlaveID(300))
   224  	return deviceInstanceOp.device
   225  }
   226  
   227  func NewDeviceModbusRTUNoStopBits(name string, namespace string) v1alpha1.Device {
   228  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   229  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withDataBits(dataBits), withParity(parity),
   230  		withSerialPort(serialPort), withSlaveID(slaveID))
   231  	return deviceInstanceOp.device
   232  }
   233  
   234  func NewDeviceModbusRTUBadStopBits(name string, namespace string) v1alpha1.Device {
   235  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   236  		withProtocolConfig(deviceProtocolModbusRTU), withBaudRate(baudRate), withDataBits(dataBits), withParity(parity),
   237  		withSerialPort(serialPort), withStopBits(3), withSlaveID(slaveID))
   238  	return deviceInstanceOp.device
   239  }
   240  
   241  func NewDeviceModbusTCP(name string, namespace string) v1alpha1.Device {
   242  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   243  		withProtocolConfig(deviceProtocolModbusTCP), withTCPServerIP("127.0.0.1"), withTCPPort(8080), withTCPSlaveID("1"))
   244  	return deviceInstanceOp.device
   245  }
   246  
   247  func NewDeviceModbusTCPNoIP(name string, namespace string) v1alpha1.Device {
   248  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   249  		withProtocolConfig(deviceProtocolModbusTCP), withTCPPort(8080), withTCPSlaveID("1"))
   250  	return deviceInstanceOp.device
   251  }
   252  
   253  func NewDeviceModbusTCPNoPort(name string, namespace string) v1alpha1.Device {
   254  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   255  		withProtocolConfig(deviceProtocolModbusTCP), withTCPServerIP("127.0.0.1"), withTCPSlaveID("1"))
   256  	return deviceInstanceOp.device
   257  }
   258  
   259  func NewDeviceModbusTCPNoSlaveID(name string, namespace string) v1alpha1.Device {
   260  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   261  		withProtocolConfig(deviceProtocolModbusTCP), withTCPPort(8080), withTCPServerIP("127.0.0.1"))
   262  	return deviceInstanceOp.device
   263  }
   264  
   265  func NewDeviceOpcUA(name string, namespace string) v1alpha1.Device {
   266  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   267  		withProtocolConfig(deviceProtocolOPCUA), withOPCUAServerURL("http://test-opcuaserver.com"))
   268  	return deviceInstanceOp.device
   269  }
   270  
   271  func NewDeviceOpcUANoURL(name string, namespace string) v1alpha1.Device {
   272  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withDeviceModelReference(DeviceModelRef),
   273  		withProtocolConfig(deviceProtocolOPCUA))
   274  	return deviceInstanceOp.device
   275  }
   276  
   277  func NewDeviceNoModelReference(name string, namespace string) v1alpha1.Device {
   278  	deviceInstanceOp := newDeviceOp(withDeviceName(name), withDeviceNamespace(namespace), withProtocolConfig(deviceProtocolOPCUA))
   279  	return deviceInstanceOp.device
   280  }