File size: 3,059 Bytes
476455e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
# language governing permissions and limitations under the License.
from __future__ import absolute_import

from sagemaker._studio import (
    _append_project_tags,
    _find_config,
    _load_config,
    _parse_tags,
)


def test_find_config(tmpdir):
    path = tmpdir.join(".sagemaker-code-config")
    path.write('{"sagemakerProjectId": "proj-1234"}')
    working_dir = tmpdir.mkdir("sub")

    found_path = _find_config(working_dir)
    assert found_path == path


def test_find_config_missing(tmpdir):
    working_dir = tmpdir.mkdir("sub")

    found_path = _find_config(working_dir)
    assert found_path is None


def test_load_config(tmpdir):
    path = tmpdir.join(".sagemaker-code-config")
    path.write('{"sagemakerProjectId": "proj-1234"}')

    config = _load_config(path)
    assert isinstance(config, dict)


def test_load_config_malformed(tmpdir):
    path = tmpdir.join(".sagemaker-code-config")
    path.write('{"proj')

    config = _load_config(path)
    assert config is None


def test_parse_tags():
    tags = _parse_tags(
        {
            "sagemakerProjectId": "proj-1234",
            "sagemakerProjectName": "proj-name",
            "foo": "abc",
        }
    )
    assert tags == [
        {"Key": "sagemaker:project-id", "Value": "proj-1234"},
        {"Key": "sagemaker:project-name", "Value": "proj-name"},
    ]


def test_parse_tags_missing():
    tags = _parse_tags(
        {
            "sagemakerProjectId": "proj-1234",
            "foo": "abc",
        }
    )
    assert tags is None


def test_append_project_tags(tmpdir):
    config = tmpdir.join(".sagemaker-code-config")
    config.write('{"sagemakerProjectId": "proj-1234", "sagemakerProjectName": "proj-name"}')
    working_dir = tmpdir.mkdir("sub")

    tags = _append_project_tags(None, working_dir)
    assert tags == [
        {"Key": "sagemaker:project-id", "Value": "proj-1234"},
        {"Key": "sagemaker:project-name", "Value": "proj-name"},
    ]

    tags = _append_project_tags([{"Key": "a", "Value": "b"}], working_dir)
    assert tags == [
        {"Key": "a", "Value": "b"},
        {"Key": "sagemaker:project-id", "Value": "proj-1234"},
        {"Key": "sagemaker:project-name", "Value": "proj-name"},
    ]

    tags = _append_project_tags(
        [{"Key": "sagemaker:project-id", "Value": "proj-1234"}], working_dir
    )
    assert tags == [
        {"Key": "sagemaker:project-id", "Value": "proj-1234"},
        {"Key": "sagemaker:project-name", "Value": "proj-name"},
    ]