Debian Codegraph @

test/hs/Test/Ganeti/PyValue.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for Ganeti.Pyvalue
5
6 -}
7
8 {-
9
10 Copyright (C) 2019 Google Inc.
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.PyValue
39 ( testPyValue
40 ) where
41
42 import Test.QuickCheck
43 import qualified Test.HUnit as HUnit
44
45 import Data.List
46 import qualified Data.ByteString as BS
47 import qualified Data.ByteString.UTF8 as UTF8
48 import qualified Data.ByteString.Base64 as B64
49
50 import Test.Ganeti.TestHelper
51 import Test.Ganeti.TestCommon
52 import Test.Ganeti.Types ()
53
54 import Ganeti.PyValue
55
56 -- * Arbitrary instances
57
58 instance Arbitrary BS.ByteString where
59 arbitrary = UTF8.fromString <$> arbitrary
60
61 -- | Custom HUnit test to check the correspondence between ByteStrings and
62 -- Python bytes. We use ast.literal_eval to evaluate the byte literals and then
63 -- write the resulting bytestrings back to Haskell for comparison.
64 --
65 -- For the technical background of this unit test, check the documentation
66 -- of "case_py_compat_types" of test/hs/Test/Ganeti/Opcodes.hs
67 --
68 -- Note that System.Process.readProcessWithExitCode (used by runPython) returns
69 -- Python's stdout as a String, by calling hGetContents on the standard output
70 -- handle. This means that data is decoded using the system locale, making the
71 -- channel not 8-bit-clean when run with a non-UTF-8 locale (such as POSIX). We
72 -- could use System.Process.Typed to get past this problem, but that's an extra
73 -- dependency not used elsewhere. Instead we work around this issue by
74 -- base64-encoding the UTF-8 output on the Python side.
75 caseByteStringsToBytes :: HUnit.Assertion
76 caseByteStringsToBytes = do
77 let num_bs = 500::Int
78 bytestrings <- generate $ vectorOf num_bs (arbitrary::Gen BS.ByteString)
79 let input = intercalate "\n" $ map showValue bytestrings
80 py_stdout <-
81 runPython "from ast import literal_eval\n\
82 \import sys\n\
83 \import base64\n\
84 \for item in sys.stdin:\n\
85 \ data = literal_eval(item)\n\
86 \ sys.stdout.buffer.write(base64.b64encode(data) + b'\\n')"
87 input
88 >>= checkPythonResult
89 let decoded = map (B64.decodeLenient . UTF8.fromString) $ lines py_stdout
90 HUnit.assertEqual "Mismatch in number of returned bytestrings"
91 (length decoded) (length bytestrings)
92 mapM_ (uncurry (HUnit.assertEqual "Different result after encoding/decoding")
93 ) $ zip bytestrings decoded
94
95
96 testSuite "PyValue"
97 [ 'caseByteStringsToBytes
98 ]