Skip to content

Commit

Permalink
Adding unittests for group and host wrappers. (#324)
Browse files Browse the repository at this point in the history
NestedGroup asGroup function now returns a Group wrapper object.
Correctly labeling allocation id as id in Host SetAllocation call.
  • Loading branch information
Greg Denton authored May 18, 2019
1 parent 3d7a085 commit 0ffe7b2
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void setAllocation(HostSetAllocationRequest request,
StreamObserver<HostSetAllocationResponse> responseObserver) {
HostInterface host = getHostInterface(request.getHost());
hostManager.setAllocation(host,
adminManager.getAllocationDetail(request.getAllocationName()));
adminManager.getAllocationDetail(request.getAllocationId()));
responseObserver.onNext(HostSetAllocationResponse.newBuilder().build());
responseObserver.onCompleted();
}
Expand Down
3 changes: 1 addition & 2 deletions cuegui/cuegui/MenuActions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,8 +1123,7 @@ def deleteGroup(self, rpcObjects=None):
for group in groups:
if isinstance(group, opencue.wrappers.group.NestedGroup):
group = group.asGroup()
self.cuebotCall(opencue.wrappers.group.Group(group).delete,
"Delete Group %s Failed" % group.name)
self.cuebotCall(group.delete, "Delete Group {} Failed".format(group.name()))


class SubscriptionActions(AbstractActions):
Expand Down
2 changes: 1 addition & 1 deletion proto/host.proto
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ message HostRenameTagResponse {} // Empty
// SetAllocation
message HostSetAllocationRequest {
Host host = 1;
string allocation_name = 2;
string allocation_id = 2;
}

message HostSetAllocationResponse {} // Empty
Expand Down
6 changes: 3 additions & 3 deletions pycue/opencue/wrappers/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def __init__(self, group):

def createSubGroup(self, name):
"""Create a sub group"""
return Group(self.asGroup()).createSubGroup(name)
return self.asGroup().createSubGroup(name)

def children(self):
"""returns jobs and groups in a single array"""
Expand All @@ -198,7 +198,7 @@ def children(self):

def asGroup(self):
"""returns a Group object from this NestedGroup"""
return job_pb2.Group(
return Group(job_pb2.Group(
id=self.data.id,
name=self.data.name,
department=self.data.department,
Expand All @@ -209,4 +209,4 @@ def asGroup(self):
max_cores=self.data.max_cores,
level=self.data.level,
group_stats=self.data.stats,
)
))
10 changes: 5 additions & 5 deletions pycue/opencue/wrappers/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def setAllocation(self, allocation):
@param allocation: An allocation object
"""
self.stub.SetAllocation(
host_pb2.HostSetAllocationRequest(host=self.data, allocation_name=allocation.id()),
host_pb2.HostSetAllocationRequest(host=self.data, allocation_id=allocation.id()),
timeout=Cuebot.Timeout)

def addComment(self, subject, message):
Expand Down Expand Up @@ -149,11 +149,11 @@ def setHardwareState(self, state):
host_pb2.HostSetHardwareStateRequest(host=self.data, state=state),
timeout=Cuebot.Timeout)

def setOs(self, os):
def setOs(self, osName):
"""Sets the host os
@type os: string
@param os: os value to set host to"""
self.stub.SetOs(host_pb2.HostSetOsRequest(host=self.data, os=os),
@type osName: string
@param osName: os value to set host to"""
self.stub.SetOs(host_pb2.HostSetOsRequest(host=self.data, os=osName),
timeout=Cuebot.Timeout)

def setThreadMode(self, mode):
Expand Down
261 changes: 261 additions & 0 deletions pycue/tests/wrappers/group_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
#!/usr/bin/env python

# Copyright (c) 2018 Sony Pictures Imageworks Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import mock
import unittest

import opencue
from opencue.compiled_proto import job_pb2


TEST_GROUP_NAME = 'testGroup'


@mock.patch('opencue.cuebot.Cuebot.getStub')
class GroupTests(unittest.TestCase):

def testCreateSubGroup(self, getStubMock):
subgroupName = 'testSubgroup'
stubMock = mock.Mock()
stubMock.CreateSubGroup.return_value = job_pb2.GroupCreateSubGroupResponse(
group=job_pb2.Group(name=subgroupName))
getStubMock.return_value = stubMock

group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
subgroup = group.createSubGroup(subgroupName)

stubMock.CreateSubGroup.assert_called_with(
job_pb2.GroupCreateSubGroupRequest(group=group.data, name=subgroupName),
timeout=mock.ANY)
self.assertEqual(subgroup.name(), subgroupName)

def testDelete(self, getStubMock):
stubMock = mock.Mock()
stubMock.Delete.return_value = job_pb2.GroupDeleteResponse()
getStubMock.return_value = stubMock

group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.delete()

stubMock.Delete.assert_called_with(
job_pb2.GroupDeleteRequest(group=group.data),
timeout=mock.ANY)

def testSetName(self, getStubMock):
stubMock = mock.Mock()
stubMock.SetName.return_value = job_pb2.GroupSetNameResponse()
getStubMock.return_value = stubMock

newName = 'changedName'
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.setName(newName)

stubMock.SetName.assert_called_with(
job_pb2.GroupSetNameRequest(group=group.data, name=newName),
timeout=mock.ANY)

def testSetMaxCores(self, getStubMock):
stubMock = mock.Mock()
stubMock.SetMaxCores.return_value = job_pb2.GroupSetMaxCoresResponse()
getStubMock.return_value = stubMock

value = 523
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.setMaxCores(value)

stubMock.SetMaxCores.assert_called_with(
job_pb2.GroupSetMaxCoresRequest(group=group.data, max_cores=value),
timeout=mock.ANY)

def testSetMinCores(self, getStubMock):
stubMock = mock.Mock()
stubMock.SetMinCores.return_value = job_pb2.GroupSetMinCoresResponse()
getStubMock.return_value = stubMock

value = 2
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.setMinCores(value)

stubMock.SetMinCores.assert_called_with(
job_pb2.GroupSetMinCoresRequest(group=group.data, min_cores=value),
timeout=mock.ANY)

def testSetDefaultJobPriority(self, getStubMock):
stubMock = mock.Mock()
stubMock.SetDefaultJobPriority.return_value = job_pb2.GroupSetDefJobPriorityResponse()
getStubMock.return_value = stubMock

value = 500
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.setDefaultJobPriority(value)

stubMock.SetDefaultJobPriority.assert_called_with(
job_pb2.GroupSetDefJobPriorityRequest(group=group.data, priority=value),
timeout=mock.ANY)

def testSetDefaultJobMaxCores(self, getStubMock):
stubMock = mock.Mock()
stubMock.SetDefaultJobMaxCores.return_value = job_pb2.GroupSetDefJobMaxCoresResponse()
getStubMock.return_value = stubMock

value = 523
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.setDefaultJobMaxCores(value)

stubMock.SetDefaultJobMaxCores.assert_called_with(
job_pb2.GroupSetDefJobMaxCoresRequest(group=group.data, max_cores=value),
timeout=mock.ANY)

def testSetDefaultJobMinCores(self, getStubMock):
stubMock = mock.Mock()
stubMock.SetDefaultJobMinCores.return_value = job_pb2.GroupSetDefJobMinCoresResponse()
getStubMock.return_value = stubMock

value = 2
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.setDefaultJobMinCores(value)

stubMock.SetDefaultJobMinCores.assert_called_with(
job_pb2.GroupSetDefJobMinCoresRequest(group=group.data, min_cores=value),
timeout=mock.ANY)

def testGetGroups(self, getStubMock):
stubMock = mock.Mock()
stubMock.GetGroups.return_value = job_pb2.GroupGetGroupsResponse(
groups=job_pb2.GroupSeq(groups=[job_pb2.Group(name=TEST_GROUP_NAME)]))
getStubMock.return_value = stubMock

group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
groups = group.getGroups()

stubMock.GetGroups.assert_called_with(
job_pb2.GroupGetGroupsRequest(group=group.data),
timeout=mock.ANY)
self.assertEqual(len(groups), 1)
self.assertEqual(groups[0].name(), TEST_GROUP_NAME)

def testGetJobs(self, getStubMock):
jobName = 'testJob'
stubMock = mock.Mock()
stubMock.GetJobs.return_value = job_pb2.GroupGetJobsResponse(
jobs=job_pb2.JobSeq(jobs=[job_pb2.Job(name=jobName)]))
getStubMock.return_value = stubMock

group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
jobs = group.getJobs()

stubMock.GetJobs.assert_called_with(
job_pb2.GroupGetJobsRequest(group=group.data),
timeout=mock.ANY)
self.assertEqual(len(jobs), 1)
self.assertEqual(jobs[0].name(), jobName)

def testReparentJobs(self, getStubMock):
stubMock = mock.Mock()
stubMock.ReparentJobs.return_value = job_pb2.GroupReparentJobsResponse()
getStubMock.return_value = stubMock

testJob = job_pb2.Job(name='testJob')
testNestedJob = job_pb2.NestedJob(name='testNestedJob')
jobs = [opencue.wrappers.job.Job(testJob), opencue.wrappers.job.NestedJob(testNestedJob)]
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.reparentJobs(jobs)

expected = job_pb2.JobSeq(jobs=[job_pb2.Job(name='testJob'),
job_pb2.Job(name='testNestedJob',
job_stats=job_pb2.JobStats())])
stubMock.ReparentJobs.assert_called_with(
job_pb2.GroupReparentJobsRequest(group=group.data, jobs=expected),
timeout=mock.ANY)

def testReparentGroups(self, getStubMock):
stubMock = mock.Mock()
stubMock.ReparentGroups.return_value = job_pb2.GroupReparentGroupsResponse()
getStubMock.return_value = stubMock

groups = [job_pb2.Group()]
groupSeq = job_pb2.GroupSeq(groups=groups)
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.reparentGroups(groups)

stubMock.ReparentGroups.assert_called_with(
job_pb2.GroupReparentGroupsRequest(group=group.data, groups=groupSeq),
timeout=mock.ANY)

def testSetDepartment(self, getStubMock):
stubMock = mock.Mock()
stubMock.SetDepartment.return_value = job_pb2.GroupSetDeptResponse()
getStubMock.return_value = stubMock

dept = 'pipeline'
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.setDepartment(dept)

expected = job_pb2.Group(name=TEST_GROUP_NAME)
stubMock.SetDepartment.assert_called_with(
job_pb2.GroupSetDeptRequest(group=expected, dept=dept),
timeout=mock.ANY)

def testSetGroup(self, getStubMock):
stubMock = mock.Mock()
stubMock.SetGroup.return_value = job_pb2.GroupSetGroupResponse()
getStubMock.return_value = stubMock

parentGroup = job_pb2.Group(name='parentGroup')
group = opencue.wrappers.group.Group(
job_pb2.Group(name=TEST_GROUP_NAME))
group.setGroup(parentGroup)

stubMock.SetGroup.assert_called_with(
job_pb2.GroupSetGroupRequest(group=group.data, parent_group=parentGroup),
timeout=mock.ANY)


@mock.patch('opencue.cuebot.Cuebot.getStub')
class NestedGroupTests(unittest.TestCase):

def testAsGroup(self, getStubMock):
nestedGroup = opencue.wrappers.group.NestedGroup(job_pb2.NestedGroup(
id='ngn-ngng-ngn',
name='testNestedGroup',
department='pipeline'
))
group = nestedGroup.asGroup()
self.assertEqual(nestedGroup.data.id, group.data.id)
self.assertEqual(nestedGroup.data.name, group.data.name)
self.assertEqual(nestedGroup.data.department, group.data.department)



if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit 0ffe7b2

Please sign in to comment.